C++ solution using hashtable


  • 0
    G
    class Solution {
    public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int, int> m;
        vector<int> res;
        if(nums1.empty()||nums2.empty()) return res;
        int len1=nums1.size(),len2=nums2.size();
        
        for(int i=0;i<len1;i++){
            m[nums1[i]]++;
        }
        for(int i=0;i<len2;i++){
            if(m[nums2[i]]>0){
                res.push_back(nums2[i]);
                m[nums2[i]]--;
            }
        }
        return res;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.