diff options
| author | Andrew Shitov <andy@shitov.ru> | 2020-08-23 11:31:13 +0200 |
|---|---|---|
| committer | Andrew Shitov <andy@shitov.ru> | 2020-08-23 11:31:13 +0200 |
| commit | cd991d6d7285cbcaa488f9695b0c43f08db563b8 (patch) | |
| tree | c55b7348b06f89a2694be72dc663b11553b067ed /challenge-074 | |
| parent | 45d1e1e55de48f5316915fe3de2a0faeaaf6ca11 (diff) | |
| download | perlweeklychallenge-club-cd991d6d7285cbcaa488f9695b0c43f08db563b8.tar.gz perlweeklychallenge-club-cd991d6d7285cbcaa488f9695b0c43f08db563b8.tar.bz2 perlweeklychallenge-club-cd991d6d7285cbcaa488f9695b0c43f08db563b8.zip | |
Move a C++ solution to its folder
Diffstat (limited to 'challenge-074')
| -rw-r--r-- | challenge-074/ash/cpp/ch-1.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-074/ash/cpp/ch-1.cpp b/challenge-074/ash/cpp/ch-1.cpp new file mode 100644 index 0000000000..74d78a32c3 --- /dev/null +++ b/challenge-074/ash/cpp/ch-1.cpp @@ -0,0 +1,33 @@ +/* + Task 1 from + https://perlweeklychallenge.org/blog/perl-weekly-challenge-074/ + + Comments: https://andrewshitov.com/2020/08/18/the-weekly-challenge-for-74/ + + Compile as: + $ g++ --std=c++17 ch-1.cpp +*/ + +#include <iostream> +#include <vector> +#include <map> +#include <algorithm> + +using namespace std; + +int main() { + vector<int> data = {1, 2, 2, 3, 2, 4, 2}; + // vector<int> data = {1, 3, 1, 2, 4, 5}; + + map<int, int> frequency; + int max_frequency = 0; + int major = 0; + for (auto x : data) { + if (++frequency[x] > max_frequency) { + max_frequency = frequency[x]; + major = x; + } + } + + cout << (max_frequency > data.size() / 2 ? major : -1) << endl; +} |
