C# Odd Occurrences In Array
static int? OddOccurrencesInArray(int[] a)
{
var count = new Dictionary<int, int>();
foreach (var value in a)
{
if (!count.ContainsKey(value))
{
count[value] = 1;
}
else
{
count.Remove(value);
}
}
return count.Count > 0 ? count.Keys.First() : (int?)null;
}
This uses XOR to cancel out pairs, leaving only the value that appears an odd number of times.