Arrays - Find missing numbers in a Sequence

后端 未结 15 1442
小鲜肉
小鲜肉 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 05:08

    const findMissing = (arr) => {
    const min = Math.min(...arr);
    const max = Math.max(...arr);
    // add missing numbers in the array
    let newArr = Array.from(Array(max-min), (v, i) => {
        return i + min
    });
    // compare the full array with the old missing array
    let filter = newArr.filter(i => {
        return !arr.includes(i)
    })
    return filter;
    };
    

提交回复
热议问题