Articles in categories

Articles

Check if array of numbers is sequential

Simple method to check if an array of numbers is sequential. Returns true or false.

public static bool IsSequential(int[] array)
{
	return array.Zip(array.Skip(1), (a, b) => (a + 1) == b).All(x => x);
}

Usage:

int[] t = { 0, 1, 2, 3, 4, 5, 6 };
bool isSeq = IsSequential(t);
// returns true

int[] t = { 0, 1, 2, 3, 3, 4, 5, 6 };
bool isSeq = IsSequential(t);
// returns false

int[] t = { 0, 1, 2, 4, 5, 6 };
bool isSeq = IsSequential(t);
// returns false