Intersection of Two Arrays II

2017/12/12 posted in  leetcode

class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> map;
        vector<int> res;
        for(auto num1:nums1){
            map[num1]++;
        }

        for(auto num2:nums2){
            if (map[num2]>0) {
                map[num2]--;
                res.push_back(num2);
            }
        }
        return res;
    }
};