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