Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Get non duplicates in a list [duplicate]

Tags:

c#

With a list like so:

int[] numbers = {1,2,2,3,3,4,4,5};

I can remove the duplicates using the Distinct() function so the list will read: 1,2,3,4,5

however, I want the inverse. I want it to remove all of the numbers that are duplicated leaving me with the unique ones.

So the list will read: 1,5.

How would this be done?

like image 603
user1662290 Avatar asked Sep 20 '13 09:09

user1662290


1 Answers

One way would be

var singles = numbers.GroupBy(n => n)
                     .Where(g => g.Count() == 1)
                     .Select(g => g.Key); // add .ToArray() etc as required
like image 167
Jon Avatar answered Nov 12 '22 22:11

Jon