public class Test {
public static void main(String[] args) {
int[] chars = {1, 2, 1, 4, 1, 6, 1, 8, 1, 10, 1};
System.out.println(moreThanHalfNum(chars));
}
public static int moreThanHalfNum(int[] nums) {
int count = 0;
int candidate = 0;
for (int num : nums) {
if (count == 0) {
candidate = num;
}
count += (num == candidate) ? 1 : -1;
}
return checkMoreThanHalf(nums, candidate) ? candidate : 0;
}
private static boolean checkMoreThanHalf(int[] array, Integer number) {
int times = 0;
for (int i : array) {
if (i == number) {
times++;
}
}
return times * 2 >= array.length;
}
}