diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-08-23 12:46:17 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-08-23 12:46:17 +0100 |
| commit | 71fd465b4068f074590bfcba989648b2777216cd (patch) | |
| tree | f58bca35a3d737ec44b80a411235d09141321bbf /challenge-074/ash/cpp/ch-1.cpp | |
| parent | 3f45db18a20e36ee0771718b15e465f8728d4877 (diff) | |
| parent | fd9637ca51b7ba5ddfd5ca90b54b03c8df779c9d (diff) | |
| download | perlweeklychallenge-club-71fd465b4068f074590bfcba989648b2777216cd.tar.gz perlweeklychallenge-club-71fd465b4068f074590bfcba989648b2777216cd.tar.bz2 perlweeklychallenge-club-71fd465b4068f074590bfcba989648b2777216cd.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-074/ash/cpp/ch-1.cpp')
| -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; +} |
