public class Solution { public int MoreThanHalfNum_Solution(int [] array) { if (array == null || array.length < 1) { return -1; } int n = array.length; int cand = 0; int cnt = 0; for (int num : array) { if (cnt == 0) { cand = num; cnt++; } else if (cand == num) { cnt++; } else { cnt--; } } cnt = 0; for (int num : array) { if (cand == num) { cnt++; } } return cnt > n / 2 ? cand : -1; } }