How To Remove Duplicates from List Using LINQ

By Henri Parviainen

Remove duplicates from list using linq

In C# you can use LINQ to remove duplicates in a List.

Remove duplicates from List

Let's say we have the following List:

List<string> greetings = new List<string>() {
  "Hey",
  "Hello",
  "Hi",
  "Hi",
  "Hello"
};

Now, what if we would want to get all unique values out of the List without any duplicates?

LINQ provides us with a simple solution for this.

To use LINQ you have to import it first by adding

using System.Collections.Generic;

to the top of your file.

Then we can use Distinct method provided by LINQ to remove the duplicates.

 var noDuplicates = greetings.Distinct().ToList();

This will create a new List without any duplicates.

We have to call ToList() because Distinct() returns an IEnumerable collection.

We can check the output of our new noDuplicates variable with the following code.

foreach(var i in noDuplicates) {
    Console.WriteLine(i);
}

The output would be:

"Hey"
"Hello"
"Hi"

There you go. That's how you can remove duplicates in Lists using LINQ.

The complete code

using System;
using System.Collections.Generic;
using System.Linq;

namespace RemoveDuplicates
{
    class Program
    {
        public static void Main(string[] args)
        {
            List<string> greetings = new List<string>() {
                "Hey",
                "Hello",
                "Hi",
                "Hi",
                "Hello"
            };

            var noDuplicates = greetings.Distinct().ToList();

            foreach(var i in noDuplicates) {
                 Console.WriteLine(i);
            }
        }
    }
}

Remove duplicates based on specific object value in a List

What if we have objects in our List and we want to remove objects that only have one equal value with another object in a List?

Take a look at the following code.

using System.Collections.Generic;
using System.Linq;
using System;

public class Program {

    public static void Main() {

        List<Item> items = new List<Item>();

        items.Add(new Item { ID = 1 , Name = "iPhone 12" });

        items.Add(new Item { ID = 2 , Name = "iPhone 12 mini" });

        items.Add(new Item { ID = 3 , Name = "iPhone 12" });

        items.Add(new Item { ID = 4 , Name = "DELL Laptop" });

        List<Item> distinctItems = items
                                    .GroupBy(i => i.Name)
                                    .Select(g => g.First())
                                    .ToList();

        foreach(var item in distinctItems) {
            System.Console.WriteLine(item.Name);
        }
    }

    public class Item
    {
        public int ID { get;set; }
        public string Name { get;set; }
    }
}

Here we have a list of items and although they don't have the same id, items with id 1 and 3 are basically the same.

We can remove all duplicates like this by using GroupBy and Select methods provided by LINQ.

First, we group our list so that we will create groups for each name in the List.

Then we use Select to only select the first objects in each group.

This will result in the removal of the duplicates.

SHARE