Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the count of duplicate items in a C# List

Tags:

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

like image 778
Priyank Thakkar Avatar asked Mar 28 '12 14:03

Priyank Thakkar


People also ask

How do you count duplicates in array?

To count the duplicates in an array:Declare an empty object variable that will store the count for each value. Use the forEach() method to iterate over the array. On each iteration, increment the count for the value by 1 or initialize it to 1 .

How many times repeat number in array in C?

To count total duplicate elements in given array we need two loops. Run an outer loop loop from 0 to size . Loop structure must look like for(i=0; i<size; i++) . This loop is used to select each element of array and check next subsequent elements for duplicates elements using another nested loop.

How do you count repeat?

1) Split the total number of reps into thirds This is currently the way I count reps: If I'm doing a set of 8, I'll count “1, 2, 3” twice and then “1, 2” at the end. If I'm doing a set of 10, I'll count to four twice and then have just two at the end. By narrowing my focus, the set seems to go by much quicker.


3 Answers

Try this:

var numberOfTestcasesWithDuplicates = 
    scenarios.GroupBy(x => x.ScenarioID).Count(x => x.Count() > 1);
like image 164
Daniel Hilgarth Avatar answered Oct 01 '22 11:10

Daniel Hilgarth


As a first idea:

int dupes = list.Count() - list.Distinct(aTestCaseComparer).Count();
like image 25
Henk Holterman Avatar answered Oct 01 '22 10:10

Henk Holterman


To just get the duplicate count:

int duplicateCount = scenarios.GroupBy(x => x.ScenarioID)
                              .Sum(g => g.Count()-1);
like image 24
BrokenGlass Avatar answered Oct 01 '22 11:10

BrokenGlass