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?
One way would be
var singles = numbers.GroupBy(n => n)
.Where(g => g.Count() == 1)
.Select(g => g.Key); // add .ToArray() etc as required
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With