I am looking at LeetCode problem 349. Intersection of Two Arrays:
Given two integer arrays
nums1andnums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
I solved it using a brute force algorithm, but then I found this solution:
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
map<int,int> mp;
vector<int> ans;
for(int x : nums1)
mp[x]=1;
for(int x: nums2)
if(mp[x]==1)
mp[x]++;
for(auto x: mp)
if(x.second>1)
ans.push_back(x.first);
return ans;
}
};
And now my question: What's the significance mp[x]=1; in that solution?