-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_test2.cpp
More file actions
49 lines (39 loc) · 1.04 KB
/
io_test2.cpp
File metadata and controls
49 lines (39 loc) · 1.04 KB
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
#include <iostream>
#include <queue>
#include <unordered_map>
#include <utility>
using namespace std;
template <class T1, class T2> bool isExists(T1 &container, T2 check_val) {
if (container.find(check_val) == container.end())
return false;
else
return true;
}
class Compare {
public:
bool operator()(pair<int, int> p1, pair<int, int> p2) {
return p1.second < p2.second;
}
};
void find_k_freq(vector<int> &array, int k) {
unordered_map<int, int> freq_vals;
priority_queue<pair<int, int>, vector<pair<int, int>>, Compare> pq;
for (auto elem : array) {
freq_vals[elem]++;
}
// can use bucket sort for linear time
// number of buckets can be atmost size of input array
// each bucket represents frequency values
for (auto pairs : freq_vals)
pq.push(pairs);
for (int it = 0; it < k; it++) {
cout << pq.top().first << endl;
pq.pop();
}
return;
}
int main(void) {
vector<int> array{1, 1, 1, 2, 2, 3, 4, 5, 6, 6, 6, 6};
find_k_freq(array, 4);
return 0;
}