[linq] C# LINQ find duplicates in List

To find the duplicate values only :

var duplicates = list.GroupBy(x => x.Key).Any(g => g.Count() > 1);

E.g.

var list = new[] {1,2,3,1,4,2};

GroupBy will group the numbers by their keys and will maintain the count (number of times it repeated) with it. After that, we are just checking the values who have repeated more than once.

To find the unique values only :

var unique = list.GroupBy(x => x.Key).All(g => g.Count() == 1);

E.g.

var list = new[] {1,2,3,1,4,2};

GroupBy will group the numbers by their keys and will maintain the count (number of times it repeated) with it. After that, we are just checking the values who have repeated only once means are unique.