C #, LINQ. How to find an item in an item group

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

I need a sub-array only with those that match the condition data[i] > data[i-1] && data[i] > data[i + 1]... i.e. I need all the elements that are tied to their nearest neighbors.

From the above example, I should get { 2, 3 }

Can this be done in LINQ?

thank

+3
source share
1 answer
data.Where((val, index)=>(index == 0 || val > data[index - 1]) 
                      && (index == data.Length - 1 || val > data[index + 1]));
+11
source

Source: https://habr.com/ru/post/1746630/


All Articles