Preview:
class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        unordered_map<int,int>mp;
        vector<int> ans;
        for(int i = 0; i < nums2.size(); ++i)   mp[nums2[i]]++;
    
        for(int i = 0; i < nums1.size(); ++i)   { 
            if(mp[nums1[i]] > 0)    {
                mp[nums1[i]]--;
                ans.push_back(nums1[i]);
            }
        }
        return ans;
    }
};
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter