My question is similar to this one: Finding Consecutive Items in List using Linq. Except, I’d like to get the last consecutive items that have no gaps. For example:
2, 4, 7, 8
output
7,8
Another example:
4,5,8,10,11,12
output
10,11,12
How can that be done?
I’m assuming in that you want the last consecutive sequence with more than one member… so from the sequence
you’re expecting the sequence:
I’ve indicated the line to remove if the last sequence is permitted to have only a single member, giving a sequence of
Here’s the linq:
There’s a hidden assumption here that the numbers of the sequence are in ascending order. If this isn’t the case, it will be necessary to enroll the powers of microsoft’s extension method for finding contiguous items in a sequence. Let me know if this is the case.