12ms c++ solution using unordered_map


  • 0
    vector<int> intersect(vector<int>& a, vector<int>& b) {
            unordered_map<int, int> t;
            vector<int> ans;
            int m=a.size(), n=b.size();
            
            for(int i=0; i<m; ++i) t[a[i]]++;
            
            for(int i=0; i<n; ++i){
                if(t.find(b[i]) != t.end() && t[b[i]]>0){
                    ans.push_back(b[i]);
                    t[b[i]]--;
                }
            }
    
            return ans;
        }

Log in to reply
 

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