tags:

views:

76

answers:

1

Imagine you have int[] data = new int [] { 1, 2, 1, 1, 3, 2 }

I need sub-array with only those which conform to a condition data[i] > data[i-1] && data[i] > data[i + 1]... i.e. I need all items which stick over their immediate neighbours.

From example above I should get { 2, 3 }

Can it be done in LINQ?

Thanks

+9  A: 
data.Where((val, index)=>(index == 0 || val > data[index - 1]) 
                      && (index == data.Length - 1 || val > data[index + 1]));
Matthew Flaschen
thanks a lot! ..
Bobb