EN
C# / .NET - remove duplicates from List using HashSet
0 points
In this article, we would like to show you how to remove duplicates from List using HashSet in C#.
Quick solution:
xxxxxxxxxx
1
List<string> myList = new List<string> { "A", "A", "B", "B" };
2
3
HashSet<string> myHashset = new HashSet<string>(myList);
In this example, we create HashSet from the List to remove duplicates.
xxxxxxxxxx
1
using System;
2
using System.Collections.Generic;
3
4
public class Program
5
{
6
public static void Main()
7
{
8
List<string> myList = new List<string> { "A", "A", "B", "B" };
9
10
HashSet<string> myHashset = new HashSet<string>(myList);
11
12
foreach (string item in myHashset)
13
Console.WriteLine(item);
14
}
15
}
Output:
xxxxxxxxxx
1
A
2
B