Search code examples
c#linq

LINQ: select elements that only appear once in a list


I have a list of objects, can be of any type T.

How to select a list of objects that appear in that list only once using linq? For example, if my list is {2,3,4,5,8,2,3,5,4,2,3,4,6}, then the output should be {6,8}.


Solution

  • You could try this:

    int[] arr = { 2, 3, 4, 5, 8, 2, 3, 5, 4, 2, 3, 4, 6 };
    var q =
        from g in arr.GroupBy(x => x)
        where g.Count() == 1
        select g.First();