-
Notifications
You must be signed in to change notification settings - Fork 0
/
2032. Two Out of Three.cpp
45 lines (37 loc) · 975 Bytes
/
2032. Two Out of Three.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
// https://leetcode.com/problems/two-out-of-three/
class Solution
{
vector<int> result;
unordered_map<int, int> freq;
unordered_set<int> numbers1;
unordered_set<int> numbers2;
unordered_set<int> numbers3;
public:
vector<int> twoOutOfThree(vector<int> &nums1, vector<int> &nums2, vector<int> &nums3)
{
Insert(numbers1, nums1);
Insert(numbers2, nums2);
Insert(numbers3, nums3);
countFreq(numbers1);
countFreq(numbers2);
countFreq(numbers3);
Filter();
return result;
}
void Filter()
{
for (auto &pair : freq)
if (pair.second >= 2)
result.push_back(pair.first);
}
void countFreq(unordered_set<int> &nums)
{
for (const int &num : nums)
freq[num]++;
}
void Insert(unordered_set<int> &nums, vector<int> &givenNums)
{
for (int &num : givenNums)
nums.insert(num);
}
};