18

Let's say that an array is sequential when each successful element has the value of previous element + 1. Suppose I have an array of numbers like {5,6,7,8} (sequential) or {1,2,5} (not sequential).

Is there a nice functional way to check if the array is sequential? I can do it with the following code:

bool IsSequential(int[] array)
{
    for (int i = 1; i < array.Length; i++)
       if (array[i] != array[i - 1] + 1)
            return false;
    return true;
}

I'm trying to determine if a poker hand is straight.

5
  • possible duplicate of make sure array is sequential in C# Commented Aug 14, 2013 at 7:02
  • How do you deal with the ace? If its value is 1, you can't use this method to detect an ace-high straight. Commented Aug 14, 2013 at 7:06
  • 3
    Your code is simple and work perfectly Commented Aug 14, 2013 at 7:10
  • 1
    Just use the code you already have - it's fine. Commented Aug 14, 2013 at 7:45
  • Is an empty array may be considered sequential? Commented Sep 21, 2018 at 6:40

8 Answers 8

22

Try this one:

    bool IsSequential(int[] array)
    {
        return array.Zip(array.Skip(1), (a, b) => (a + 1) == b).All(x => x);
    }
Sign up to request clarification or add additional context in comments.

3 Comments

@AlexeiLevenkov: If you don't use Skip, you're just comparing elements with themselves, which of course will always be equal.
Just to add, this will not work if the array of ints is not in order. e.g. this would work with [4,5,6] but not with [5,4,6]. You can just use array.OrderBy(a => a).Zip(array.Skip(1), (a, b) => (a + 1) == b).All(x => x); to get the latter to work.
For this to also work for decreasing sequential arrays, use return array.Zip(array.Skip(1), (a, b) => (a + 1) == b).All(x => x) || array.Zip(array.Skip(1), (a, b) => (a - 1) == b).All(x => x);
8

I don't know if it's really an improvement/nicer but you could use Range.

ENumerable.Range(0, myArray.Length).Any(i => myArray[i] != myArray[0] + i)

This returns true if the array doesn't contain sequential number.

1 Comment

Nice, although if we want to check if an array is sequential: Enumerable.Range(0, myArray.Length).All(i => myArray[i] == myArray[0] + i)
2

Using Linq:

    public static bool IsSequential(int[] a)
    {
        return Enumerable.Range(1, a.Length - 1).All(i => a[i] - 1 == a[i - 1]);
    }

Comments

2

This should do the trick, for all sequential, non sequential data. A complete example with sample input. Tested and works fine

var list = new List<int>(new[] { 7, 6, 5, 4, 3,9});
int minValue = list.Min();
int maxValue = list.Count;
List<int> test =  Enumerable.Range(minValue, maxValue).ToList();
var result = Enumerable.Range(minValue, maxValue).Except(list);
if (result.ToList().Count == 0)
{
  Console.WriteLine("numbers are in sequence");
}
else
{               
   Console.WriteLine("Numbers are not in sequence");
 }

Comments

1
var result = Enumerable.Range(array[0], array[array.Length-1]).Except(array.ToList());

Comments

1

Same as: make sure array is sequential in C#

Answer there:

if you're sure that the array is sorted and has no duplicates, you can just check:

array[array.Length - 1] == array[0] + array.Length - 1

2 Comments

That's wrong, with this you can't know anything about the other elements than first and last.
@Serge that's why specified: if you're sure that the array is sorted and has no duplicates. If so - the answer is correct. Think why!
1

Try this one, I think it is most simple:

nums.Select((n, i) => n - i).Distinct().Count() == 1

Comments

0

First sort the array, remove N of a kind (e.g. pairs) using distinct() and If the array length is always == to 5 All you need to do is if((array[4] - array[0]) == 4) return true.

It gets more complicated if its texas holdem or if you need to account for both an ace high and ace low straight.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.