C# Dominator
static int Dominator(int[] a)
{
var size = 0;
var value = 0;
var index = 0;
for (int k = 0; k < a.Length; k++)
{
var v = a[k];
if (size == 0)
{
size++;
value = v;
index = k;
}
else if (value != v)
{
size--;
}
else
{
size++;
}
}
var candidate = size > 0 ? value : -1;
var count = 0;
foreach (var v in 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.