Code : Print Intersection
You have been given two integer arrays/lists (ARR1 and ARR2) of size N and M, respectively. You need to print their intersection; An intersection for this problem can be defined when both the arrays/lists contain a particular value or to put it in other words, when there is a common value that exists in both the arrays/lists.
Note :
Input format :
Output format :
Constraints :
Sample Input 1 :
Sample Output 1 :
Sample Input 2 :
Sample Output 2 :
2
#include<bits/stdc++.h>
void printIntersection(int arr1[], int arr2[], int n, int m) {
// Write your code here
unordered_map<int,int>mp;
for(int i=0;i<n;i++)
mp[arr1[i]]++;
for(int i=0;i<m;i++){
if(mp.count(arr2[i])>0){
cout<<arr2[i]<<endl;
mp[arr2[i]]--;
if(mp[arr2[i]]==0) mp.erase(arr2[i]);
}
}
// for(int i=0;i<m;i++){
// if(mp.count(arr2[i])>0){// present hai ke nahi
// cout<<arr2[i]<<endl;
// mp[arr2[i]]--;
// if(mp[arr2[i]]==0) mp.erase(arr2[i]);
// }
// }
}
Comments
Post a Comment