You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
int n1 = nums1.size() , n2 = nums2.size();
unordered_set<int> set;
vector<int> ans;
for(int i=0;i<n1;i++)
set.insert(nums1[i]);
for (int i = 0; i < n2; i++) {
// If element from nums2 exists in set, push it to ansif (set.find(nums2[i]) != set.end()) {
ans.push_back(nums2[i]);
// Erasing from set can be done after processing all elements
set.erase(nums2[i]);
}
}
return ans;
}
};