Removing duplicate items from List collection.

Rajesh_Kumar
Posted by Rajesh_Kumar under C# category on | Points: 40 | Views : 1343
With the help of Distinct method of list collection,we can remove duplicate values from list.

List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(3);
list.Add(4);
list.Add(4);
list.Add(4);

foreach (int value in list)
{
Console.WriteLine("Before: {0}", value);
}

// Get distinct elements and convert into a list again.
List<int> distinct = list.Distinct().ToList();

foreach (int value in distinct)
{
Console.WriteLine("After: {0}", value);
}

Comments or Responses

Login to post response