-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintersectionArray.cpp
More file actions
52 lines (42 loc) · 894 Bytes
/
intersectionArray.cpp
File metadata and controls
52 lines (42 loc) · 894 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
#include <vector>
#include <unordered_set>
#include <unordered_map>
using namespace std;
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
unordered_set<int> input, output;
for (const int& i : nums1) {
input.insert(i);
}
for (const int& i : nums2) {
if (input.find(i) != input.end()) {
output.insert(i);
}
}
vector<int> v(output.begin(), output.end());
return v;
}
int majorityElement(vector<int>& nums) {
pair<int, int> p;
unordered_map<int, int> map;
for (const int& i: nums) {
map[i] += 1;
}
for (auto i : map) {
if (i.second > p.second ) {
p = i;
}
}
return p.first;
}
};
int main()
{
Solution s;
vector<int> a = { 1, 2, 3, 1, 1, 2}, b = { 1, 2};
auto v = s.intersection(a, b);
auto val = s.majorityElement(a);
printf("majorityElem %d \n", val);
return 0;
}