Usually a simple loop can be written to remove duplicates. A nested for-loop can execute much faster than the Distinct method on an int array.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
class Program
{
static IEnumerable<int> Test1(int[] array)
{
// Use distinct to check for duplicates.
return array.Distinct();
}
static IEnumerable<int> Test2(int[] array)
{
// Use nested loop to check for duplicates.
List<int> result = new List<int>();
for (int i = 0; i < array.Length; i++)
{
// Check for duplicates in all following elements.
bool isDuplicate = false;
for (int y = i + 1; y < array.Length; y++)
{
if (array[i] == array[y])
{
isDuplicate = true;
break;
}
}
if (!isDuplicate)
{
result.Add(array[i]);
}
}
return result;
}
static void Main()
{
int[] array1 = { 1, 2, 2, 3, 4, 4 };
const int _max = 1000000;
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
// Version 1: benchmark distinct.
var result = Test1(array1);
if (result.Count() != 4)
{
break;
}
}
s1.Stop();
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
// Version 2: benchmark nested loop.
var result = Test2(array1);
if (result.Count() != 4)
{
break;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString(
"0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString(
"0.00 ns"));
}
}
185.44 ns Distinct method
51.11 ns Nested for-loops