Find Duplicates in a List in C#

Saad Aslam Jan 30, 2023 Aug 09, 2022
  1. Use Enumerable.GroupBy() to Find Duplicates in a List in C#
  2. Use HashSet to Find Duplicates in a List in C#
Find Duplicates in a List in C#

Through reading this article, we will have a better understanding of the steps that must be taken to locate duplicate entries in a list using the C# programming language.

The following are the two approaches that may be used to complete this task:

  • Enumerable.GroupBy()
  • HashSet

Use Enumerable.GroupBy() to Find Duplicates in a List in C#

We can use the Enumerable.GroupBy() function to group the elements according to each element’s value. Then, a filter removes the groups that only exist once, leaving the remaining groups with duplicate keys.

To begin, we will have to import the essential libraries for the functions to utilize in the implementation.

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

We will construct a new list of the string type and give it the name dataList. We will add some items to the list we just created within the Main() function.

List<string> dataList = new List<string>() {"Saad", "John", "Miller", "Saad", "Stacey"};

Now, we need to initialize a variable checkDuplicates of IEnumerable<string> type that uses .GroupBy(), .Where(), and .Select() functions. This process will check for duplicates in the dataList.

If there are any, they will be stored in the checkDuplicates variable.

IEnumerable<string> checkDuplicates = dataList.GroupBy(x => x)
                                .Where(g => g.Count() > 1)
                                .Select(x => x.Key);

We will use the condition to determine whether or not any duplicates have been saved in the checkDuplicates.

If this condition is not met, the message No duplicate elements in the list will be shown in the console instead of the elements being written out. On the other hand, if the condition is met, duplicate items will be displayed.

if(checkDuplicates.Count() > 0)
{
    Console.WriteLine("The duplicate elements in the list are: " + String.Join(", ", checkDuplicates));
} else {
    Console.WriteLine("No duplicate elements in the list");
}

Complete Source Code:

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

public class DuplicatesBySaad
{
    public static void Main()
    {
        List<string> dataList = new List<string>() {"Saad", "John", "Miller", "Saad", "Stacey"};

        IEnumerable<string> checkDuplicates = dataList.GroupBy(x => x)
                                        .Where(g => g.Count() > 1)
                                        .Select(x => x.Key);

        if(checkDuplicates.Count() > 0)
        {
            Console.WriteLine("The duplicate elements in the list are: " + String.Join(", ", checkDuplicates));
        } else {
            Console.WriteLine("No duplicate elements in the list");
        }
    }
}

Output:

The duplicate elements in the list are: Saad

Use HashSet to Find Duplicates in a List in C#

In most cases, we make use of it when we wish to stop the collection from being populated with pieces that are duplicates of others. Compared to the list’s performance, the HashSet has substantially superior overall performance.

Create a list dataList of type string to store string data in it.

List<string> dataList = new List<string>() {"Saad", "John", "Miller", "Saad", "Stacey"};

After that, we have to create a HashSet with the name hashSet that is of type string and then initialize it before we can save the data from the list.

In addition, you need to create an IEnumerable variable of string type and give it the name duplicateElements. This variable should check the dataList and, if it finds any duplicates, it should add and save them.

And lastly, we shall print the duplicate elements to the console.

HashSet<string> hashSet = new HashSet<string>();
IEnumerable<string> duplicateElements = dataList.Where(e => !hashset.Add(e));

Console.WriteLine("The duplicate elements in the list are: " + String.Join(", ", duplicateElements));

Complete Source Code:

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

public class DuplicatesBySaad
{
    public static void Main()
    {
        List<string> dataList = new List<string>() {"Saad", "John", "Miller", "Saad", "Stacey"};

        HashSet<string> hashSet = new HashSet<string>();
        IEnumerable<string> duplicateElements = dataList.Where(e => !hashSet.Add(e));

        Console.WriteLine("The duplicate elements in the list are: " + String.Join(", ", duplicateElements));
    }
}

Output:

The duplicate elements in the list are: Saad
Author: Saad Aslam
Saad Aslam avatar Saad Aslam avatar

I'm a Flutter application developer with 1 year of professional experience in the field. I've created applications for both, android and iOS using AWS and Firebase, as the backend. I've written articles relating to the theoretical and problem-solving aspects of C, C++, and C#. I'm currently enrolled in an undergraduate program for Information Technology.

LinkedIn

Related Article - Csharp List