1287. 有序数组中出现次数超过25%的元素
1287. 有序数组中出现次数超过25%的元素
题目链接:1287. 有序数组中出现次数超过25%的元素
代码如下:
class Solution {
public:
int findSpecialInteger(vector<int>& arr) {
unordered_map<int, int> um;
for (int i = 0;i < arr.size();i++) {
um[arr[i]]++;
}
for (auto it = um.begin();it != um.end();it++) {
double rate = (double)it->second / arr.size();
if (rate >= 0.25) {
return it->first;
}
}
return 0;
}
};