Arrays - Find missing numbers in a Sequence

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

    let missing = [];
    let numArray = [3,5,1,8,9,36];
    const sortedNumArray = numArray.sort((a, b) => a - b);
    sortedNumArray.reduce((acc, current) => {
      let next = acc + 1;
      if (next !== current) {
        for(next; next < current; next++) {
          missing.push(next);
        }
      }
      return current;
    });

提交回复
热议问题