#include <algorithm> #include <unordered_map> // 集計する std::unordered_map<unsigned int, size_t> hash; for(const auto &x : data){ if(hash.find(x) != hash.end()){ ++hash.at(x); }else{ hash[x] = 1; } } // 最大値の要素のインデックスを取り出す // 別途比較関数を書きたくなかったのでラムダ式にした // (ラムダ式の引数でautoが使えるのはC++14から) auto max_iterator2 = std::max_element(hash.begin(), hash.end(), [](const auto &a, const auto &b) -> bool { return (a.second < b.second); } ); int mode = max_iterator2->first; std::cout << "最頻値:" << mode << std::endl;