-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path349_array_intersection.cpp
55 lines (43 loc) · 1.1 KB
/
349_array_intersection.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
// Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
// TC = O(n) and SC = O(n)
#include <bits/stdc++.h>
using namespace std;
vector<int> intersection(vector<int> &nums1, vector<int> &nums2) {
unordered_set<int> s1;
unordered_set<int> s2;
//insert all elements from nums1 to set 1
for(int i =0; i<nums1.size(); i++) {
s1.insert(nums1[i]++);
}
//find all the elements of nums2 in set1 and insert common elements into set2
for(int i = 0; i<nums2.size(); i++) {
int k = nums2[i];
if(s1.find(k) != s1.end()) {
s2.insert(k);
}
}
vector<int> ans;
for(auto itr = s2.begin(); itr != s2.end(); itr++) {
ans.push_back(*itr);
}
return ans;
}
int main() {
vector<int> n1;
vector<int> n2;
for(int i =0; i<4; i++) {
int a;
cin>> a;
n1.push_back(a);
}
for(int i =0; i<2; i++) {
int a ;
cin >> a;
n2.push_back(a);
}
vector<int> ans = intersection(n1, n2);
for(auto itr: ans) {
cout<< itr << " ";
}
return 0;
}