How to remove duplicates from a list in C#
This tutorial explains how to remove duplicates from a list in C#.
We will introduce three different ways how to remove duplicates from a list.
When using the List <T> class, you may want to manage data without duplicate elements. However, with the current List<T> class, duplicate elements can be added.
Learn more:
Determining with List<T>.Contains(T) before adding elements
When using List <T>, you must declare the following in the using statement.
using System.Collections.Generic;
As a standard practice, List<T>.Contains(T) can be used to prevent duplication of elements in a list.
When adding an element to a list, the List<T>.Contains(T) method can be used to determine if the element already exists in the list.
The following sample program creates a new list named new_list, loops through the list named list, and adds each element to new_list if it does not exist.
using System; using System.Collections.Generic; namespace CSharpExample { public class Program { public static void Main(string[] args) { List<string> list = new List<string> { "A1", "A2", "A2", "A1", "A2", "A3", "A4", "A3" }; List<string> new_list = new List<string>(); foreach (string item in list) { //If the item does not exist, add it to "new_list" if (!new_list.Contains(item)) { new_list.Add(item); } } //Display items in the list foreach (string item in new_list) { Console.WriteLine("Item: {0}", item); } } } }
Output:
Item: A1
Item: A2
Item: A3
Item: A4
Removing duplicates from a list using C#'s HashSet class
In C#, the HashSet class is used to create sets. A set is a collection of individual objects that have no specific order. That is, the elements of a set are not repeated and unordered. You can eliminate repeated elements from a list by putting the list into a HashSet and then using LINQ's ToList() function to return that HashSet to the list. The following code shows how to remove duplicate elements from a list using the class HashSet:
using System; using System.Collections.Generic; using System.Linq; namespace CSharpExample { public class Program { public static void Main(string[] args) { List<string> list = new List<string> { "A4", "A1", "A2", "A2", "A1", "A2", "A3", "A4", "A3", "A5", "A6" }; //Initialize a HashSet instance HashSet<string> hashset = new HashSet<string>(list); //Convert it to a list using ToList() method List<string> new_list = hashset.ToList(); //Display items in the list foreach (string item in new_list) { Console.WriteLine("Item: {0}", item); } } } }
Output:
Item: A4
Item: A1
Item: A2
Item: A3
Item: A5
Item: A6
Removing duplicates from a list using C#'s LINQ Distinct() method
LINQ's Distinct() method is used to select unique values from a C# data structure, and the ToList() function converts a collection of elements into a C# list. The following code sample illustrates how to remove duplicate values from a list using C# LINQ techniques:
using System; using System.Linq; using System.Collections.Generic; namespace CSharpExample { public class Program { public static void Main(string[] args) { //Initialize an empty list named "list" List<string> list = new List<string>(); //Add elements to the list list.Add("A1"); list.Add("A1"); list.Add("A2"); list.Add("A2"); list.Add("A2"); list.Add("A3"); list.Add("A3"); list.Add("A4"); list.Add("A4"); list.Add("A5"); list.Add("A6"); //Remove duplicates and return a new list named "new_list" List<string> new_list = list.Distinct().ToList(); //Display items in the list foreach (string item in new_list) { Console.WriteLine("Item: {0}", item); } } } }
Output:
Item: A1
Item: A2
Item: A3
Item: A4
Item: A5
Item: A6
Summary
This C# tutorial introduced how to remove duplicates from a list using the C# LINQ Distinct() method, HashSet class, and List<T>.Contains(T) method.
Reference: Enumerable.Distinct Method