https://leetcode.com/problems/intersection-of-two-arrays/
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2]
.
Note:
- Each element in the result must be unique.
- The result can be in any order.
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
vector<int> res;
unordered_map<int,bool> s;
if (nums1.size() == 0 || nums2.size() == 0){
return res;
}
for (int n : nums1){
s[n] = true;
}
for (int n : nums2){
if (s.count(n)&&s[n]==true){
res.push_back(n);
s[n] = false;
}
}
return res;
}
};