codeLink:https://ide.codingblocks.com/s/186193
I have implemented everything right, than also don’t able to understand y my multiset is storing only one value, becuse it should store duplicate too, please look into my code, correct it and specify my mistake
Arrays intersection of two arrays(hashing)
hey @Divya_321 the problem is that when you iterate map, each element comes once only. So use one map as an count array. Also you need to print [ braces before and after the end of the solution
link to your code according the things mentioned above
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
@Divya_321 the link is working please check it again, in case you still face the same problem i have pasted code here
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin>>t;
unordered_map<long long, long long> umap1, umap2;
int temp;
for(long long i = 0; i < t; i++){
cin>>temp;
umap1[temp]++; //using map as a count array
}
int a[t];
for(long long i = 0; i < t; i++){
// cin>>temp;
// umap2[temp] = 1;
cin >> a[i];
}
multiset<long long > s;
// for(auto i = umap1.begin(); i != umap1.end(); i++){
// int p = i ->first;
// if(umap2.count(p))
// s.insert(p);
// }
for(int i = 0; i < t; i++){
if(umap1[a[i]] > 0) umap1[a[i]]--, s.insert(a[i]);
}
multiset<long long > :: iterator it;
vector <int> res;
for(it = s.begin(); it!= s.end(); it++)
res.push_back(*it);
cout << "[";
for(int i = 0; i < res.size(); i++)
if(i < res.size() - 1)
cout << res[i] << ", ";
else
cout << res[i] << "]";
}