Java Dominator
public class Solution {
public static int dominator(int[] a) {
int size = 0;
int value = 0;
int index = 0;
for (int k = 0; k < a.length; k++) {
int v = a[k];
if (size == 0) {
size++;
value = v;
index = k;
} else if (value != v) {
size--;
} else {
size++;
}
}
int candidate = size > 0 ? value : -1;
int count = 0;
for (int v : a) {
if (v == candidate) {
count++;
}
}
if (count <= a.length / 2.0) {
index = -1;
}
return index;
}
}
This finds a value that appears in more than half of the array, then returns one valid index for it.