using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading.Tasks;
public static int UseLinq(int[] input)
.Where( g => g.Count() %2 == 1)
public static int UseSort(int[] input)
if (input[0] != input[1]) return input[0];
for ( int i = 1; i < input.GetUpperBound(0); i++ )
if (input[i] == input[i+1]) continue;
if (input[i] == input[i-1]) continue;
public static int BruteForce(int[] input)
for (int i=0; i<=input.GetUpperBound(0); i++)
for (int j=0; j<=input.GetUpperBound(0); j++)
if (input[i] == input[j])
if (matchCount % 2 == 0) return input[i];
public static int WithDictionary(int[] input)
var counts = new Dictionary<int, int>();
if (!counts.ContainsKey(n))
foreach (var d in counts)
if (d.Value % 2 == 1) return d.Key;
public static int WithDictionaryAndLinq(int[] input)
var counts = input.GroupBy( n => n).ToDictionary( g => g.Key, g=> g.Count() );
foreach (var d in counts)
if (d.Value % 2 == 1) return d.Key;
public static int WithPairTracking(int[] input)
var odd = new List<int>();
if (odd.Contains(n)) odd.Remove(n);
public static int WithParallelLoop(int[] input)
if (n == element) found = !found;
Interlocked.Exchange(ref result, element);
public static int ByRemovingFromList(int[] input)
var list = input.ToList();
public static int ByRemovingFromArray(int[] input)
while (i <= input.GetUpperBound(0))
var j = Array.IndexOf(input, n, ++i);
public static void Main()
9,3,9,3,9,7,9,7,11,11,8
Console.WriteLine("By removing: {0}", ByRemovingFromList(A));
Console.WriteLine("Using LINQ: {0}", UseLinq(A));
Console.WriteLine("By sorting first: {0}", UseSort(A));
Console.WriteLine("Brute force: {0}", BruteForce(A));
Console.WriteLine("With dictionary: {0}", WithDictionary(A));
Console.WriteLine("With dictionary and LINQ: {0}", WithDictionaryAndLinq(A));
Console.WriteLine("By removing from array: {0}", ByRemovingFromArray(A));
Console.WriteLine("With pair tracking: {0}", WithPairTracking(A));
Console.WriteLine("Parallel: {0}", WithParallelLoop(A));