题解 | #数组中出现次数超过一半的数字#
数组中出现次数超过一半的数字
http://www.nowcoder.com/practice/e8a1b01a2df14cb2b228b30ee6a92163
public class Solution {
public static int MoreThanHalfNum_Solution(int [] array) {
int len=array.length;
int times=len/2;
int res=0;
HashMap<Integer,Integer> maps=new HashMap<>();
for(int i=0;i<len;i++){
maps.put(array[i], maps.getOrDefault(array[i], 0)+1);
if(maps.get(array[i])>times){
res=array[i];
break;
}
}
return res;
}
}