TheDeveloperBlog.com

Home | Contact Us

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

C# Remove Duplicates in List

This C# program removes duplicate elements from a List collection.

Remove duplicates. Programs sometimes must remove duplicate List elements.

There are many ways to remove these elements. Some are easier to implement than others. One approach uses the Distinct extension method from System.Linq.

Example. First, this program requires a newer .NET Framework, because it uses the System.Linq namespace. In Main, a List with seven int elements is created. But the List contains duplicate elements for the values 3 and 4.

Int

Tip: By using the Distinct parameterless extension method on the List type, we can remove those duplicate elements.

Then: We can optionally invoke the ToList extension to get an actual List with the duplicates removed.

ToList

C# program that removes duplicates in List

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
	// List with duplicate elements.
	List<int> list = new List<int>();
	list.Add(1);
	list.Add(2);
	list.Add(3);
	list.Add(3);
	list.Add(4);
	list.Add(4);
	list.Add(4);

	foreach (int value in list)
	{
	    Console.WriteLine("Before: {0}", value);
	}

	// Get distinct elements and convert into a list again.
	List<int> distinct = list.Distinct().ToList();

	foreach (int value in distinct)
	{
	    Console.WriteLine("After: {0}", value);
	}
    }
}

Output

Before: 1
Before: 2
Before: 3
Before: 3
Before: 4
Before: 4
Before: 4
After: 1
After: 2
After: 3
After: 4

Distinct. We review the Distinct extension method. Found in the System.Linq namespace, this method can also be invoked on other types of collections, such as arrays. It acts upon types that implement IEnumerable.

DistinctIEnumerable

Also: It can specify an equality comparer, which it uses to determine what elements are equal and can be made distinct.

Summary. LINQ extension methods are useful on List collections. Developers often favor the List type over the array type for its resizable behavior. So the LINQ extension methods such as Distinct may be more commonly invoked on this type.

Tip: For erasing duplicate elements in a list and making every element unique, you can employ the Distinct extension method.


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