Concise C++ solution using map.


  • 0
    B
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
            {
                map<int, bool> table;
                
                for(int i = 0; i < nums1.size(); ++i) table[nums1[i]] = true;
                
                vector<int> ret;
                for(int i = 0; i < nums2.size(); ++i)
                {
                    if(table[nums2[i]])
                    {
                        ret.push_back(nums2[i]);
                        table[nums2[i]] = false;
                    }
                }
                return ret;
            }

Log in to reply
 

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