2215. Find the Difference of Two Arrays

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
vector<vector<int>> findDifference(vector<int>& nums1, vector<int>& nums2) {
vector<vector<int>> ret(2, vector<int>(0));
unordered_set<int> num1, num2;
for(int n:nums1) num1.insert(n);
for(int n:nums2) num2.insert(n);
for(auto it=num1.begin() ; it!=num1.end() ; ++it)
{
if(!num2.count(*it)) ret[0].push_back(*it);
}

for(auto it=num2.begin() ; it!=num2.end() ; ++it)
{
if(!num1.count(*it)) ret[1].push_back(*it);
}
return ret;
}
};

analysis

  • time complexity O(n)
  • space complexity O(n)