0

I have a list like this:

var list = new List<string>() { "a", "b", "c", "a" };

As you can see I have one duplicate inside this list.

The desired output should look like this:

a (2)
b (1)
c (1)

The code which I already have looks like this:

list.GroupBy(x => x).Select(x => x.Key + "(" + x.Count() + ")").ToList().ForEach(x => list2.Add(x));

But as you can see I need a second list to get the desired output. My question now is how can I get the desired output with using only one list.

Trevor
  • 7,777
  • 6
  • 31
  • 50
fireaxe
  • 1
  • 1
  • 2
    I don't understand. If you just get rid of `.Foreach` and everything after it, isn't the resulting list exactly what you want? – Kirk Woll Mar 16 '22 at 15:20
  • Just store the result of your `Select`-statement into a list, omit the foreach and you're done. – MakePeaceGreatAgain Mar 16 '22 at 15:21
  • 1
    `list2 = list.GroupBy(x => x).Select(x => x.Key + "(" + x.Count() + ")").ToList()` – maraaaaaaaa Mar 16 '22 at 15:21
  • Does this answer your question? [How to Count Duplicates in List with LINQ](https://stackoverflow.com/questions/454601/how-to-count-duplicates-in-list-with-linq) – Trevor Mar 16 '22 at 15:30

4 Answers4

1

Why don't we just add the items into existing list2?

var list2 = new List<string>();

...

list2.AddRange(list
  .GroupBy(item => item)
  .Select(group => $"{group.Key} ({group.Count()})"));

Or create list2 from scratch:

var list2 = list
  .GroupBy(item => item)
  .Select(group => $"{group.Key} ({group.Count()})")
  .ToList();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

This does what you need:

var list = new List<string>() { "a", "b", "c", "a" };
foreach (var group in list.GroupBy(b => b).OrderBy(g => g.Key))
{
    Console.WriteLine($"{group.Key} ({group.Count()})");
}

Consequentially, if one wishes to dedup this list, one could do:

list = list.GroupBy(b => b).Select(g => g.Key).ToList();

The result would be a deduped list with "a", "b" and "c" in it.

JuanR
  • 7,405
  • 1
  • 19
  • 30
0

You can do this in one statement (I've split it over multiple lines for readability):

var list = new List<string> { "a", "b", "c", "a" };

Console.WriteLine(
    string.Join("\n", 
        list.GroupBy(_=>_).Select(g => $"{g.Key} ({g.Count()})")));

Output:

a (2)
b (1)
c (1)
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
0

You can use the Distinct method to remove duplicates. This returns a IEnumerable and in order to use the ForEach method you have to apply the ToList method first. The Foreach iterates over the items {"a", "b", "c"} and prints the current item and the count of this item in the list with dulicates.

list.Distinct().ToList().ForEach(v1 => Console.WriteLine($"{v1} ({list.Count(v2 => v1 == v2)})"))
Don CorliJoni
  • 223
  • 1
  • 8
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Mar 17 '22 at 00:48