版权声明:菜鸟一枚~~ 有想法可在下面评论, 转载标明出处即可。 https://blog.csdn.net/KLFTESPACE/article/details/87571703
中途用到了vector。
#include<iostream>
#include<map>
#include <algorithm>
#include <vector>
int cmp(const std::pair<int, int>& x, const std::pair<int, int>& y)
{
return x.second > y.second;
}
void sortMapByValue(std::map<int, int>& tMap, std::vector<std::pair<int, int> >& tVector)
{
for (std::map<int, int>::iterator curr = tMap.begin(); curr != tMap.end(); curr++)
{
tVector.push_back(std::make_pair(curr->first, curr->second));
}
std::sort(tVector.begin(), tVector.end(), cmp);
}
int main()
{
int T, n;
std::cin >> T;
while(T--){
std::cin >> n;
std::map<int, int> a;
int tmp;
for(int i=0; i<n; i++){
std::cin >> tmp;
a[tmp]++;
// std::cout << tmp << ":" << a[tmp] << std::endl;
}
/*
if(n == 1){
std::cout << tmp << std::endl;
continue;
}
*/
std::vector<std::pair<int,int> > t;
sortMapByValue(a, t);
// std::cout << t.size() << std::endl;
for(int i=0; i < t.size(); i++){
// std::cout << t[i].first << ":" << t[i].second << std::endl;
if(t[i].second == t[0].second && i != t.size()-1){
tmp = t[i].first;
continue;
}
else {
if(i == t.size()-1)
tmp = t[i].first;
std::cout << tmp << std::endl;
break;
}
}
}
return 0;
}