1、思想就是目标元素在数组中超过一半,这样在数组中,两个不同元素都相互抵消不断执行的话一定能剩下目标元素 2、算法中,我们只定义x为遍历过程中的假定值,不断增加它的数量,减少它的数量。如果不够减就替换为新的值,以此来求出目标值。

public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        if (array.length == 1) {
            return array[0];
        }
        int x = -1; int num = 0;
        for (int i = 0; i < array.length; i++) {
            if(num > 0){
                if(array[i] != x){
                    num--;
                    continue;
                }else{
                    num++;
                    continue;
                }
            }else{
                num++;
                x = array[i];
            }

        }

        return x;
    }
}