Skip to content

Instantly share code, notes, and snippets.

@brilligence
Last active December 15, 2015 05:09
Show Gist options
  • Save brilligence/5207191 to your computer and use it in GitHub Desktop.
Save brilligence/5207191 to your computer and use it in GitHub Desktop.
Using LINQ to find duplicates (List extension)
/// <summary>
/// Method that returns all the duplicates in the collection.
/// </summary>
/// <typeparam name="T">The type.</typeparam>
/// <param name="list">The Original List to detect for duplicates.</param>
/// <returns>A list of duplicates found in the Original List.</returns>
public static List<T> GetDuplicates<T>(this List<T> list)
{
// Finding duplicates by Grouping the entries and then finding
// Groups that have more than one entity
var duplicateList = list.GroupBy(i => i).Where(j => j.Count() > 1).Select(j => j.Key);
return duplicateList.ToList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment