🎉 One-stop destination for all your technical interview Preparation 🎉
Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.
class Solution{
public:
vector<int> intersect(vector<int> &nums1, vector<int> &nums2)
{
int n1 = nums1.size(), n2 = nums2.size();
vector<int> ans;
for (int i = 0; i < n1; i++){
for (int j = 0; j < n2; j++){
if (nums1[i] == nums2[j]){
ans.emplace_back(nums2[j]);
nums2[j] = -1;
break;
}
}
}
return ans;
}
};