diff options
| author | deadmarshal <adeadmarshal@gmail.com> | 2022-11-02 14:15:24 +0330 |
|---|---|---|
| committer | deadmarshal <adeadmarshal@gmail.com> | 2022-11-02 14:15:24 +0330 |
| commit | 0c702901ccc86d908ce0bb77de5698f555b3c958 (patch) | |
| tree | 4ccf650bbc60ee55e531a326ae682cac9b4e74bf /challenge-189/deadmarshal/cpp/ch-2.cpp | |
| parent | 51c585599f0d5a08899e0380f216a15be14b1167 (diff) | |
| download | perlweeklychallenge-club-0c702901ccc86d908ce0bb77de5698f555b3c958.tar.gz perlweeklychallenge-club-0c702901ccc86d908ce0bb77de5698f555b3c958.tar.bz2 perlweeklychallenge-club-0c702901ccc86d908ce0bb77de5698f555b3c958.zip | |
challenge189
Diffstat (limited to 'challenge-189/deadmarshal/cpp/ch-2.cpp')
| -rw-r--r-- | challenge-189/deadmarshal/cpp/ch-2.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-189/deadmarshal/cpp/ch-2.cpp b/challenge-189/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..ced6366898 --- /dev/null +++ b/challenge-189/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,51 @@ +#include<iostream> +#include<vector> +#include<unordered_map> + +void array_degree(const std::vector<int>& vec) +{ + std::unordered_map<int,int> left{},count{}; + std::size_t x{},min{},max{},index{}; + for(std::size_t i = 0; i < vec.size(); ++i) + { + x = vec[i]; + if(!count.contains(x)) + { + left[x] = i; + count[x] = 1; + } + else count[x]++; + + if(count[x] > max) + { + max = count[x]; + min = i - left[x] + 1; + index = left[x]; + } + else if((count[x] == max) && (i - left[x] + 1 < min)) + { + min = i - left[x] + 1; + index = left[x]; + } + } + + for(std::size_t i = index; i < index+min; ++i) + std::cout << vec[i] << ' '; + std::cout << '\n'; +} + +int main() +{ + std::vector<int> + vec1{1,3,3,2}, + vec2{1,2,1,3}, + vec3{1,3,2,1,2}, + vec4{1,1,2,3,2}, + vec5{2,1,2,1,1}; + array_degree(vec1); + array_degree(vec2); + array_degree(vec3); + array_degree(vec4); + array_degree(vec5); + return 0; +} |
