169. 多数元素 - 力扣(LeetCode)
哈希表来找出这个数出现几次
class Solution {
public:
int majorityElement(vector<int>& nums) {
unordered_map<int,int> counts;
int cnt = 0, majority = 0;
for(int num : nums){
++counts[num];
if(counts[num] > cnt){
majority = num;
cnt = counts[num];
}
}
return majority;
}
};