-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_4.cpp
More file actions
58 lines (51 loc) · 1.32 KB
/
2_4.cpp
File metadata and controls
58 lines (51 loc) · 1.32 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
50
51
52
53
54
55
56
57
#include <iostream>
#include <pcg_random.hpp>
#include <random>
#include <unordered_map>
#include <vector>
using namespace std;
int main() {
cout.precision(4);
int N = 0;
vector<int> arr;
// cin >> N;
// for (int i = 0; i < N; i++) {
// double tmp;
// cin >> tmp;
// arr.push_back(tmp);
// }
// generate test data
pcg_extras::seed_seq_from<std::random_device> seed_source;
pcg32 rng(seed_source);
std::uniform_int_distribution dist(1, 10);
N = 10;
for (int i = 0; i < N; i++)
arr.push_back(dist(rng));
cout << "arr size:" << arr.size() << endl;
for (const auto &i: arr)
cout << i << ", ";
cout << endl;
unordered_map<int, int> map;
for (const int &i: arr) {
if (!map.contains(i)) {
map[i] = 1;
} else {
map[i] = map[i] + 1;
}
}
int modeValue = 0, modeCount = 0;
for (const auto &[fst, snd]: map) {
if (snd > modeCount) {
modeCount = snd;
modeValue = fst;
}
}
// ranges::sort (arr);
// for (auto &i : arr)
// cout << i << ", ";
// cout << endl;
// cout << "modeValue:" << modeValue << ", modeCount:" << modeCount << endl;
cout << endl << "answer:" << endl;
cout << modeValue << endl;
return 0;
}