Arrays - Find missing numbers in a Sequence

后端 未结 15 1450
小鲜肉
小鲜肉 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

    If you know that the numbers are sorted and increasing:

    for(var i = 1; i < numArray.length; i++) {
        if(numArray[i] - numArray[i-1] != 1) {
               //Not consecutive sequence, here you can break or do whatever you want
        }
    }
    

提交回复
热议问题