0

I have some LINQ code (unfortunatley not to hand!) to get duplicates from a list object. However, the duplicates in the original list are like so:

Item A
Item A
Item A
Item B
Item B
Item C
Item C
Item C
Item C

etc...

What I'd like to do is extend the LINQ query to get all of those duplicate instances, not just the first instance in a duplicate pair, or trio.

What would be an ideal query to get this?

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
  • 4
    Is `GroupBy` not sufficient? – tzaman Feb 15 '15 at 23:57
  • The given answers are correct, but I note that you can be considerably more memory-efficient if you know that the list containing duplicates is sorted so that all the duplicates are together -- as is the case in your sample list. Do you in fact know ahead of time that the list will be in this form? – Eric Lippert Mar 11 '15 at 23:28

2 Answers2

5

You can group on the property and get the groups that has more than one item. Example:

List<IGrouping<Item>> duplicates =
  items.GroupBy(i => i.Name).Where(g => g.Count() > 1).ToList();

Now each group contains all the duplicates.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks. The List is actually storing just string objects, so would I just change i.Name to i? Thanks – GurdeepS Feb 16 '15 at 00:03
  • 1
    @dotnetdev: Yes, you would. However, if there is no more information that the string, then there's little point in getting all the duplicate items as they will be identical. – Guffa Feb 16 '15 at 00:07
0

You could also use the ToLookup method to do the same:

var duplicates= Items.ToLookup(s => s).Where(g=>g.Count()>1);

If you want to see the differences between the GroupBy and ToLookup, I suggest you check this link to see the answer of @JonSkeet

Community
  • 1
  • 1
ocuenca
  • 38,548
  • 11
  • 89
  • 102