思想比较简单:
1、选定一个元素,作为出现次数超过一半的元素a,计次数i为1
2、依次与后续元素比较,若存在相等元素则i+1,反之i-1
3、若i==0,令当前元素为a,重复步骤1
private static int halfNumber(int[] array) { if (array == null || array.length == 0) { return 0; } int element = 0, times = 0; for (int i = 0; i < array.length; i++) { if (times == 0) { times++; element = array[i]; } else if (element == array[i]) { times++; } else { times--; } } return element; }