350. Intersection of Two Arrays II
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]// Hashmap
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { // time: O(m + n); space: O(m)
vector<int> res;
unordered_map<int, int> mp;
for (int num : nums2) ++mp[num];
for (int num : nums1) {
if (mp[num]-- > 0) res.push_back(num);
}
return res;
}Last updated