Remove duplicates from a List in C#
This post will discuss how to remove duplicates from a list in C# without destroying the original order of the elements.
1. Using HashSet
We know that HashSet<T> does not permit any duplicate elements. Therefore, if we convert the given list (with duplicates) to HashSet<T> and then convert it back to the list, we’ll get a list with all distinct elements.
The following code example demonstrates how to use the HashSet<T> collection for removing duplicates from the list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static List<T> removeDuplicates<T>(List<T> list) { return new HashSet<T>(list).ToList(); } public static void Main() { List<int> list = new List<int> { 2, 3, 3, 4, 1, 2, 5 }; List<int> distinct = removeDuplicates(list); Console.WriteLine(String.Join(",", distinct)); } } /* Output: 2,3,4,1,5 */ |
Please note that the above solution creates a new list and destroys the original ordering of the elements. The following code demonstrates how to use HashSet<T> with List’s RemoveAll() method to in-place remove duplicates from the list and maintain the order of elements.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Collections.Generic; public static class Extentions { public static void removeDuplicates<T>(this List<T> list) { HashSet<T> hashset = new HashSet<T>(); list.RemoveAll(x => !hashset.Add(x)); } } public class Example { public static void Main() { List<int> list = new List<int> { 2, 3, 3, 4, 1, 2, 5 }; list.removeDuplicates(); Console.WriteLine(String.Join(",", list)); } } /* Output: 2,3,4,1,5 */ |
2. Using Enumerable.Distinct() method (System.Linq)
To preserve the original order, we can also use LINQ’s Distinct() method. The following code example demonstrates how to use the Distinct() to return distinct elements from a list of integers.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Collections.Generic; using System.Linq; public class Example { public static void Main() { List<int> list = new List<int> { 2, 3, 3, 4, 1, 2, 5 }; List<int> distinct = list.Distinct().ToList(); Console.WriteLine(String.Join(",", distinct)); } } /* Output: 2,3,4,1,5 */ |
3. Using Enumerable.Union() method (System.Linq)
Another solution is to use LINQ’s Union() method, which gives an IEnumerable<T> which contains the elements from two sequences, excluding duplicates. To convert the resultant sequence to a list, call the ToList() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; using System.Collections.Generic; using System.Linq; public class Example { public static void Main() { List<int> list = new List<int>() { 1, 2, 2, 3, 2, 4, 1, 4, 5 }; List<int> distinct = list.Union(list).ToList(); Console.WriteLine(String.Join(',', distinct)); } } /* Output: 1,2,3,4,5 */ |
That’s all about removing duplicates from a List in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)