2019有赞秋招,查找数组众数
查找数组众数
http://www.nowcoder.com/questionTerminal/3584a44114ea4805a9f6814e99285835
使用了hasmap求解哦
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
void findMode() {
unordered_map<int, int> ump;
int num;
while (getchar() != ']')
{
cin >> num;
ump[num]++;
}
int maxi = ump[0]; //最多出现次数
int idx = 0; //最多出现次数对应在unordered_map中的键
int len = ump.size();
for (auto t: ump) {
if (maxi < t.second) {
maxi = t.second;
idx = t.first;
}
}
cout << idx << endl;
}
int main() {
findMode();
return 0;
}
查看10道真题和解析