-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionofTwoArrays.cpp
More file actions
53 lines (42 loc) · 952 Bytes
/
IntersectionofTwoArrays.cpp
File metadata and controls
53 lines (42 loc) · 952 Bytes
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
#include<iostream>
#include<vector>
#include<unordered_set>
using namespace std;
vector<int> intersection(vector<int>& , vector<int>& );
int main()
{
ios_base::sync_with_stdio(false);
int size1;
cout<<"Size 1: ";
cin>>size1;
int size2;
cout<<"Size 2: ";
cin>>size2;
vector<int> vec1(size1), vec2(size2);
cout<<"1st Vector: ";
for(int &v1:vec1)
cin>>v1;
cout<<"2nd Vector: ";
for(int &v2: vec2)
cin>>v2;
vector<int> intersect= intersection(vec1,vec2);
cout<<"intersection of both vectors is: ";
for(int &r:intersect)
cout<<r<<" ";
cout<<endl;
return 0;
}
vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
{
unordered_set<int> set1(nums1.begin(),nums1.end());
//unordered_set<int> set2;
vector<int> result;
for(int num: nums2){
if(set1.count(num)){
result.push_back(num);
set1.erase(num);
}
}
//vector<int>result(set2.begin(),set2.end());
return result;
}