How to check if an array of elements are in Sequential Order in C#

You can use the SequenceEqual method to check if numbers are in Sequence order.  SequenceEqual method determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.

Code

//Your Input
int[] numSequence = { 1, 2, 3, 4, 5 };
//Check if they are in sequence or not
bool isInSequence = numSequence.SequenceEqual(Enumerable.Range(1, numSequence.Count()));

Above code will give the result as true if numbers are in proper sequence.

You also need to ensure that you have added the below namespace for Linq in your page

using System.Linq;
Advertisements

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: