Q:

Reduce stacks of duplicates in an array into only one of them in c#

I'll take it directly:
For example I have an array
int[] integers = {1, 2, 2, 2, 4, 5, 2, 2, 2};

And all I like is to reduce all the stacks of 2 into only one 2. I need to get this at the end:
{1, 2, 4, 5, 2};

I read about the .Distinct() method and every kind of comparers, but that method deletes all the same elements and I don't want this. I still think there shold be some certain method to do it via Linq or somethig. Thanks!

A:

Use the LINQ, Luke!
var result = integers.Where((n, i) =>
    i == 0 ? true : n != integers[i - 1]);

More concise:
var result = integers.Where((n, i) =>
    i == 0 || n != integers[i - 1]);

