How to Count Duplicates in List with Linq

How to Count Duplicates in List with LINQ

You can use "group by" + "orderby". See LINQ 101 for details

var list = new List<string> {"a", "b", "a", "c", "a", "b"};
var q = from x in list
group x by x into g
let count = g.Count()
orderby count descending
select new {Value = g.Key, Count = count};
foreach (var x in q)
{
Console.WriteLine("Value: " + x.Value + " Count: " + x.Count);
}

In response to this post (now deleted):

If you have a list of some custom objects then you need to use custom comparer or group by specific property.

Also query can't display result. Show us complete code to get a better help.

Based on your latest update:

You have this line of code:

group xx by xx into g

Since xx is a custom object system doesn't know how to compare one item against another.
As I already wrote, you need to guide compiler and provide some property that will be used in objects comparison or provide custom comparer. Here is an example:

Note that I use Foo.Name as a key - i.e. objects will be grouped based on value of Name property.

There is one catch - you treat 2 objects to be duplicate based on their names, but what about Id ? In my example I just take Id of the first object in a group. If your objects have different Ids it can be a problem.

//Using extension methods
var q = list.GroupBy(x => x.Name)
.Select(x => new {Count = x.Count(),
Name = x.Key,
ID = x.First().ID})
.OrderByDescending(x => x.Count);

//Using LINQ
var q = from x in list
group x by x.Name into g
let count = g.Count()
orderby count descending
select new {Name = g.Key, Count = count, ID = g.First().ID};

foreach (var x in q)
{
Console.WriteLine("Count: " + x.Count + " Name: " + x.Name + " ID: " + x.ID);
}

Count of duplicate items in a C# list

If you just need the total count:

var total = colorList.GroupBy(_ => _).Where(_ => _.Count() > 1).Sum(_ => _.Count());

An alternative which might be faster with large data sets:

var hashset = new HashSet<string>(); // to determine if we already have seen this color
var duplicates = new HashSet<string>(); // will contain the colors that are duplicates
var count = 0;
foreach (var color in colorList)
{
if (!hashset.Add(color))
{
count++;
if (duplicates.Add(color))
count++;
}
}

UPDATE: measured both methods with a list of 2^25 (approx. 30 million) entries: first one 3.7 seconds, second one 3.2 seconds.

C# LINQ find duplicates in List

The easiest way to solve the problem is to group the elements based on their value, and then pick a representative of the group if there are more than one element in the group. In LINQ, this translates to:

var query = lst.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(y => y.Key)
.ToList();

If you want to know how many times the elements are repeated, you can use:

var query = lst.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(y => new { Element = y.Key, Counter = y.Count() })
.ToList();

This will return a List of an anonymous type, and each element will have the properties Element and Counter, to retrieve the information you need.

And lastly, if it's a dictionary you are looking for, you can use

var query = lst.GroupBy(x => x)
.Where(g => g.Count() > 1)
.ToDictionary(x => x.Key, y => y.Count());

This will return a dictionary, with your element as key, and the number of times it's repeated as value.

How to count duplicate in linq?

You can use GroupBy:

var students = new List<string>{"John", "Mary", "John"};

foreach (var student in students.GroupBy(x => x))
{
Console.WriteLine("{0}: {1}", student.Key, student.Count());
}

Returns:

John: 2
Mary: 1

You can show the ones that have duplicates too:

var dups = students.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(g => g.Key);

foreach (var student in dups)
{
Console.WriteLine("Duplicate: {0}", student);
}

Returns:

Duplicate: John

Note: You will need to change GroupBy(x => x) depending on what your Student object is of course. In this case, it's just a string.

C# Determine Duplicate in List

Unless I'm missing something, then you should be able to get away with something simple using Distinct(). Granted it won't be the most complex implementation you could come up with, but it will tell you if any duplicates get removed:

var list = new List<string>();

// Fill the list

if(list.Count != list.Distinct().Count())
{
// Duplicates exist
}

Order by duplicate amount in a list using linq

Assuming that keyword, bkgColor and keywordColor are public properties of the class Cell. You can use this linq

var result = cells.GroupBy(c => c.bkgColor)
.OrderByDescending(g => g.Count())
.SelectMany(g => g.Select(c => c));

Find the count of duplicate items in a C# List

Try this:

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

group and count duplicates to a dictionary with linq

You are very close to solution in fact. You can also solve it such;

    var query =
from q in list
group q by q into t
select new
{
Key = t.Key,
Value = t.Count()
};
var dict = query.ToDictionary(x => x.Key, x => x.Value);


Related Topics



Leave a reply



Submit