Using LINQ, from a List
, how can I retrieve a list that contains entries repeated more than once and their values?
To find the duplicate values only :
var duplicates = list.GroupBy(x => x.Key).Any(g => g.Count() > 1);
Eg. var list = new[] {1,2,3,1,4,2};
so group by 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 uniuqe values only :
var unique = list.GroupBy(x => x.Key).All(g => g.Count() == 1);
Eg. var list = new[] {1,2,3,1,4,2};
so group by 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.