Is there a way to calculate the difference between elements in an array and check if difference is > 1 using LINQ?
So if you have an array {1,2,3,5,8,9}, want to know what the difference is between each of the elements and only get the elements that follow each up directly, so difference == 1.
Is this possible using LINQ?
You could use the
.Whereoverload that uses the index – of course this also uses the fact that numbers is an array with an index operator:Edited to capture all numbers that are part of an “island” of consecutive numbers, with an island size of > 1 – that means you have to look forward and backward from each number to find if either the predecessor or successor is consecutive.
Output is
{1,2,3,8,9}in the example case.