Solution with unordered_map


  • -1
    R
    class Solution {
    public:
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            vector<int> ans;
            unordered_map<int,int> mp;
            for(int i=0;i<nums1.size();i++){
                mp[nums1[i]]=1;
            }
            for(int i=0;i<nums2.size();i++){
                if(mp[nums2[i]]==1){
                    ans.push_back(nums2[i]);
                    mp[nums2[i]]++;
                }
            }
            return ans;
        }
    };

Log in to reply
 

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