Accepted answer

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))
        if (duplicates.Add(color))

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


If you just need the count of the duplicate items:

 List<string> colorList = new List<string> { "red", "red", "yellow", "blue", "blue", "orange", "green", "red" };

 var count = colorList.GroupBy(item => item)
                      .Where(item => item.Count() > 1)
                      .Sum(item => item.Count());

Try this for item by item details:

var result = colorList.GroupBy(item => item)
                      .Select(item => new
                              Name = item.Key,
                              Count = item.Count()
                      .OrderByDescending(item => item.Count)
                      .ThenBy(item => item.Name)


Well I would do it without group by

List<string> colorList = new List<string> { "red", "red", "yellow", "blue", "blue", "orange", "green", "red" };
        var count = 0;
        foreach (var item in colorList.Distinct().ToList())
            var cnt = colorList.Count(i => i.Equals(item, StringComparison.InvariantCultureIgnoreCase));
            if (cnt > 1)
                count += cnt;



Another way of doing the count of the duplicates items in a C# can be as follow:-

 var duplicates = from d in list
 group d by d into c
 let count = c.Count()
 orderby count descending
 select new { Value = c.Key, Count = count };

  foreach (var v in duplicates)
     string strValue = v.Value;
     int Count = v.Count;


Not as fast as the accepted answer, but for the reference one can also use a dictionary to count the hits:

var map = new Dictionary<string, int>();
foreach (var color in colorList))
    if (map.ContainsKey(color)) map[color]++;
    else map.Add(color, 1);

return map.Values.Count(x => x > 1);

It's much faster than a LINQ GroupBy

Related Articles