-2

I am looking for a faster and more accurate way to check a Sequence:

List<int> sequence = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 … 41}



private bool IsSequential(List<int> sequence)
{

    int S = sequence[0];
    int T = sequence[sequence.Count- 1];

    List<int> Possible = Enumerable.Range(S, T).ToList();
    List<int> Except = sequence.Except(Possible).ToList();

    if (Except.Count == 0)
        return true;
    else
        return false;
}

My code returns 1 if the list is the same, I have some sort of count issue?

I wonder if there is a better way to check an integer sequence: 200, 201, 202... and so on.

Some Sequences may be out of sequence: 200, 210, 203, 204... I need to identify this issue.

Thanks

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Rusty Nail
  • 2,692
  • 3
  • 34
  • 55

1 Answers1

3

You can try like following using SequenceEqual.

  List<int> sequence = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 };
  bool isInSequence = sequence.SequenceEqual(Enumerable.Range(sequence[0], sequence.Count()));
PSK
  • 17,547
  • 5
  • 32
  • 43
  • Can you explain why the use of 'sequence.Count()' is used instead of 'sequence[sequence.Count - 1]' as the last number of the sequence is the last integer? – Rusty Nail Feb 04 '19 at 07:59
  • 1
    @RustyNail, it's not the last number, it is the count of numbers to be generated while generating the sequence. – PSK Feb 04 '19 at 08:02