Priyank Thakkar
Priyank Thakkar

Reputation: 4852

Find the count of duplicate items in a C# List

I am using List in C#. Code is as mentioned below:

TestCase.cs

 public class TestCase
{
    private string scenarioID;
    private string error;

    public string ScenarioID
    {
        get
        {
            return this.scenarioID;
        }
        set
        {
            this.scenarioID = value;
        }
    }

    public string Error
    {
        get
        {
            return this.error;
        }
        set
        {
            this.error = value;
        }
    }

    public TestCase(string arg_scenarioName, string arg_error)
    {
        this.ScenarioID = arg_scenarioName;
        this.Error = arg_error;
    }
}

List I am createing is:

private List<TestCase> GetTestCases()
    {
        List<TestCase> scenarios = new List<TestCase>();
        TestCase scenario1 = new TestCase("Scenario1", string.Empty);
        TestCase scenario2 = new TestCase("Scenario2", string.Empty);
        TestCase scenario3 = new TestCase("Scenario1", string.Empty);
        TestCase scenario4 = new TestCase("Scenario4", string.Empty);
        TestCase scenario5 = new TestCase("Scenario1", string.Empty);
        TestCase scenario6 = new TestCase("Scenario6", string.Empty);
        TestCase scenario7 = new TestCase("Scenario7", string.Empty);

        scenarios.Add(scenario1);
        scenarios.Add(scenario2);
        scenarios.Add(scenario3);
        scenarios.Add(scenario4);
        scenarios.Add(scenario5);
        scenarios.Add(scenario6);
        scenarios.Add(scenario7);

        return scenarios;
    }

Now I am iterating through the list. I want to find the how many duplicate testcases are there in a list with same ScenarioID. Is there any way to solve it using Linq or any inbuilt method for List?

Regards, Priyank

Upvotes: 5

Views: 24486

Answers (6)

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174289

Try this:

var numberOfTestcasesWithDuplicates = 
    scenarios.GroupBy(x => x.ScenarioID).Count(x => x.Count() > 1);

Upvotes: 23

Servy
Servy

Reputation: 203802

var groups = scenarios.GroupBy(test => test.ScenarioID)
    .Where(group => group.Skip(1).Any());

That will give you a group for each ScenarioID that has more than one items. The count of the groups is the number of duplicate groups, and the count of each group internally is the number of duplicates of that single item.

Additional note, the .Skip(1).Any() is there because a .Count() in the Where clause would need to iterate every single item just to find out that there is more than one.

Upvotes: 4

nawfal
nawfal

Reputation: 73163

To get total number of duplicates, yet another:

var set = new HashSet<string>();
var result = scenarios.Count(x => !set.Add(x.ScenarioID));

To get distinct duplicates:

var result = scenarios.GroupBy(x => x.ScenarioID).Count(x => x.Skip(1).Any());

Upvotes: 1

Henk Holterman
Henk Holterman

Reputation: 273179

As a first idea:

int dupes = list.Count() - list.Distinct(aTestCaseComparer).Count();

Upvotes: 8

Arion
Arion

Reputation: 31239

Something like this maybe

var result= GetTestCases()
            .GroupBy (x =>x.ScenarioID)
            .Select (x =>new{x.Key,nbrof=x.Count ()} );

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 160852

To just get the duplicate count:

int duplicateCount = scenarios.GroupBy(x => x.ScenarioID)
                              .Sum(g => g.Count()-1);

Upvotes: 5

Related Questions