-
Notifications
You must be signed in to change notification settings - Fork 11
/
common_elements.cpp
57 lines (47 loc) · 1.41 KB
/
common_elements.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution{
public:
vector<int> common_element(vector<int>v1,vector<int>v2)
{
vector<int>ans;
map<int,int>hash1,hash2;
for(auto x : v1)
hash1[x]++;
for(auto x : v2)
hash2[x]++;
for(auto x : v1)
if(hash2[x]-- > 0)
ans.push_back(x);
sort(ans.begin(),ans.end());
return ans;
*************************************************************************************
unordered_map<int,int>mp;
for(int i=0;i<v1.size();i++)
mp[v1[i]]++;
vector<int>v;
for(int i=0;i<v2.size();i++)
{
if(mp.find(v2[i])!=mp.end()){
v.push_back(v2[i]);
mp[v2[i]]--;
if(mp[v2[i]]==0)
mp.erase(v2[i]);
}
}
sort(v.begin(),v.end());
return v;
*************************************************************************************
int a[100001] = {0};
vector<int> ans;
for(int i=0; i<v1.size(); i++){
a[v1[i]]++;
}
for(int i=0; i<v2.size();i++){
if(a[v2[i]]> 0){
ans.push_back(v2[i]);
a[v2[i]]--;
}
}
sort(ans.begin(), ans.end());
return ans;
}
};