TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Distinct Extension Method

This C# tutorial demonstrates the Distinct extension method from System.Linq.

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf