Arrays - Find missing numbers in a Sequence

后端 未结 15 1387
小鲜肉
小鲜肉 2021-02-01 04:08

I\'m trying to find an easy way to loop (iterate) over an array to find all the missing numbers in a sequence, the array will look a bit like the one below.

var nu

15条回答
  •  不思量自难忘°
    2021-02-01 04:48

    It would be fairly straightforward to sort the array:

    numArray.sort();
    

    Then, depending upon what was easiest for you:

    1. You could just traverse the array, catching sequential patterns and checking them as you go.
    2. You could split the array into multiple arrays of sequential numbers and then check each of those separate arrays.
    3. You could reduce the sorted array to an array of pairs where each pair is a start and end sequence and then compare those sequence start/ends to your other data.

提交回复
热议问题