Skip to content

Latest commit

 

History

History
20 lines (17 loc) · 388 Bytes

350. Intersection of Two Arrays II.md

File metadata and controls

20 lines (17 loc) · 388 Bytes
class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> m;
        vector<int> ans;
        
        for (int num : nums1)
            m[num]++;
        
        for (int num : nums2) {
            if (m[num]-- > 0) {
                ans.push_back(num);
            }
        }
        return ans;
    }
};