题目地址:数组中出现次数超过一半的数字_牛客题霸_牛客网
题目回顾:


解题思路:
最简单用哈希表来记录每个数字在数组中出现的次数,在遍历这个数组时同时进行判断是否满足条件。
整体代码:
public int MoreThanHalfNum_Solution (int[] numbers) {
HashMap<Integer,Integer> hashMap = new HashMap<>();
int n = numbers.length/2;
for (int i = 0; i < numbers.length; i++) {
if (hashMap.containsKey(numbers[i])){
hashMap.put(numbers[i],hashMap.get(numbers[i])+1);
}else
hashMap.put(numbers[i],1);
if (hashMap.get(numbers[i])>n)
return numbers[i];
}
return 0;
}







![GPU Microarch 学习笔记 [1]](https://img-blog.csdnimg.cn/9ef406ed96d04780bae1afc478a5b869.png)











