My 12ms simple C++ solution using only one unordered_map


  • 0
    Y
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {    
    vector<int> ans;    
    unordered_map<int, int>map2;    
    for (auto val2 : nums2)map2[val2]++;    
    for (auto val1 : nums1){    
    	if (map2[val1] > 0){    
    		ans.push_back(val1);    
    		map2[val1]--;    
    	}    
    }    
    return ans;    
    }

Log in to reply
 

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