#include <algorithm>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param numbers int整型vector
* @return int整型
*/
int MoreThanHalfNum_Solution(vector<int>& numbers) {
// write code here
std::unordered_map<int, int> hashMap;
for(int e:numbers){
// 检查元素是否存在
if (hashMap.find(e) != hashMap.end()) {
hashMap[e]++;
}else{
hashMap[e]=1;
}
}
// 遍历哈希表
int mx;
int count=0;
int record;
for (const auto& pair : hashMap) {
if(count==0){
mx=pair.second;
record=pair.first;
}
if(mx<pair.second){
mx=pair.second;
record=pair.first;
}
count++;
}
return record;
}
};