数组中出现次数超过一半的数字

题目:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。

https://blog.csdn.net/justlikeu777/article/details/84933246

如果有符合条件的数字,则它出现的次数比其他所有数字出现的次数和还要多。
在遍历数组时保存两个值:一是数组中一个数字,一是次数。遍历下一个数字时,若它与之前保存的数字相同,则次数加1,否则次数减1;若次数为0,则保存下一个数字,并将次数置为1。遍历结束后,所保存的数字即为所求。然后再判断它是否符合条件即可。

public class MoreThanHalf{
    public int getMoreThanHalf(int[] array){
        if(array==null || array.length==0) return -1;
        int result = array[0];
        int count = 0;
        for(int i=0;i<array.length;i++){
            if(count ==0){
                result = array[i];
                count = 1;
            }else if(result == array[i]){
                count++;
            }else{
                count--;
            }
        }
        count = 0;
        for(int i=0;i<array.length;i++){
            if(result == array[i]){
                count++;
            }
        }
        if(count > (array.length/2)){
            return result;
        }
        return -1;
    }
    public static void main(String[] args){
        int[] array = {1,2,3,2,2,2,5,4,2};
        MoreThanHalf m = new MoreThanHalf();
        int result = m.getMoreThanHalf(array);
        System.out.println(result);
    }
}
原文地址:https://www.cnblogs.com/yingpu/p/9249434.html