Last active
December 15, 2015 05:09
-
-
Save brilligence/5207191 to your computer and use it in GitHub Desktop.
Using LINQ to find duplicates (List extension)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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