Short C++ solution


  • 0
    J
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> uset;
        for(int i = 0; i<nums1.size(); i++)
            uset.insert(nums1[i]);
        
        vector<int> res;
        for(int i = 0; i<nums2.size(); i++) {
            if(uset.count(nums2[i])) {
                res.push_back(nums2[i]);
                uset.erase(nums2[i]);
            }
        }
    
        return res;
    }

Log in to reply
 

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