C# Remove Duplicates from List

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.

  1. using System;
  2. using System.Collections.Generic;
  3. namespace CSharpExample
  4. {
  5. public class Program
  6. {
  7. public static void Main(string[] args)
  8. {
  9. List<string> list = new List<string>
  10. {
  11. "A1", "A2", "A2", "A1", "A2", "A3", "A4", "A3"
  12. };
  13. List<string> new_list = new List<string>();
  14. foreach (string item in list)
  15. {
  16. //If the item does not exist, add it to "new_list"
  17. if (!new_list.Contains(item))
  18. {
  19. new_list.Add(item);
  20. }
  21. }
  22. //Display items in the list
  23. foreach (string item in new_list)
  24. {
  25. Console.WriteLine("Item: {0}", item);
  26. }
  27. }
  28. }
  29. }

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:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace CSharpExample
  5. {
  6. public class Program
  7. {
  8. public static void Main(string[] args)
  9. {
  10. List<string> list = new List<string>
  11. {
  12. "A4", "A1", "A2", "A2", "A1",
  13. "A2", "A3", "A4", "A3", "A5",
  14. "A6"
  15. };
  16. //Initialize a HashSet instance
  17. HashSet<string> hashset = new HashSet<string>(list);
  18. //Convert it to a list using ToList() method
  19. List<string> new_list = hashset.ToList();
  20. //Display items in the list
  21. foreach (string item in new_list)
  22. {
  23. Console.WriteLine("Item: {0}", item);
  24. }
  25.  
  26. }
  27. }
  28. }

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:

  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. namespace CSharpExample
  5. {
  6. public class Program
  7. {
  8. public static void Main(string[] args)
  9. {
  10. //Initialize an empty list named "list"
  11. List<string> list = new List<string>();
  12. //Add elements to the list
  13. list.Add("A1");
  14. list.Add("A1");
  15. list.Add("A2");
  16. list.Add("A2");
  17. list.Add("A2");
  18. list.Add("A3");
  19. list.Add("A3");
  20. list.Add("A4");
  21. list.Add("A4");
  22. list.Add("A5");
  23. list.Add("A6");
  24. //Remove duplicates and return a new list named "new_list"
  25. List<string> new_list = list.Distinct().ToList();
  26. //Display items in the list
  27. foreach (string item in new_list)
  28. {
  29. Console.WriteLine("Item: {0}", item);
  30. }
  31. }
  32. }
  33. }

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


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables