Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique Values from a list of of items in C#

I am having a list:

list = { 1,1,1,2,3,3,3,4,4,5,6,6,6}

Now I want to extract list of unique values.

Final list contains {2,5} only.

How can I do that through LINQ or any other function.

like image 219
sudhanshu Avatar asked Dec 26 '22 13:12

sudhanshu


1 Answers

One way would be to use the GroupBy method and filter only those which have a count of 1:

var unique = list.GroupBy(l => l)
                 .Where(g => g.Count() == 1)
                 .Select(g => g.Key);
like image 57
tvanfosson Avatar answered Jan 05 '23 19:01

tvanfosson