A LINQ Style Duplicate Item Finder

Language-Integrated Query (LINQ) includes a number of set operators, including one that removes duplicates from a sequence to return only distinct values. This article describes a custom set operator that returns only the duplicated values from a list.

Adding the Overloads

To complete the class we need to add the three remaining overloaded versions of the method. For the overloads that don't include a comparer, we use the default comparer for the type specified in the generic type parameter. For those that don't include the returnAll flag, we pass true to return every duplicate.

Calling the Methods

We can now try the four versions of the method using the code below. The first use returns all of the exact duplicates from an array of strings. The second returns only one of each duplicate. The third and fourth calls repeat the first and second using case-insensitive comparisons.