数组的主元素查询
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
int find_main(vector<int>v){
unordered_map<int,int>HashMap;
for(vector<int>::iterator it = v.begin();it!=v.end();it++){
if(HashMap.find(*it) != HashMap.end()){
HashMap[*it]++;
}else{
HashMap[*it] = 1;
}
}
int res = -1;
int resNum = 0;
for(auto it:HashMap){
if(it.second > res){
res = it.second;
resNum = it.first;
}
}
if(res > v.size()/2){
return resNum;
}else return -1;
}
int main(){
int n;
while(cin >> n && n!=0){
vector<int>v;
for(int i = 0;i<n;i++){
int x;
cin>>x;
v.push_back(x);
}
cout<<find_main(v)<<endl;
}
return 0;
}