TheDeveloperBlog.com


C# Distinct Extension Method

Distinct removes all duplicate elements in a collection. It returns only the distinct elements. The System.Linq namespace provides this extension method. Distinct returns a collection. It is useful in a variety of program contexts.


Example. In this example, we declare and allocate an array on the managed heap. The array contains six elements, but only four different numbers. Two of the elements are repeated. This fact is key to the program's output.

Int Array

Next: We apply the Distinct extension method to the array reference, and then assign the result to an implicitly typed local variable.

Var

Finally: We loop over the result and display the distinct elements in the processed array.

Console.WriteLine
C# program that removes duplicate elements

using System;
using System.Linq;

class Program
{
    static void Main()
    {
	// Declare an array with some duplicated elements in it.
	int[] array1 = { 1, 2, 2, 3, 4, 4 };
	// Invoke Distinct extension method.
	var result = array1.Distinct();
	// Display results.
	foreach (int value in result)
	{
	    Console.WriteLine(value);
	}
    }
}

Output

1
2
3
4


Discussion. The Distinct method is not ideal for all purposes, particularly those where performance is critical. Internally, the Distinct method is implemented in terms of iterators that are automatically generated by the C# compiler.

Therefore: Heap allocations occur when you invoke Distinct. For optimum performance, you could use loops on small collections.

And: With small data sets, the overhead of using iterators and allocations likely overshadows any asymptotic advantage.


Summary. We looked at the Distinct extension method, declared in the System.Linq namespace. As with other LINQ extensions, the Distinct method provides a declarative, function-oriented syntax for a typically imperative processing task.

Further: The Distinct extension incurs practical performance drawbacks in some program contexts.