Arrays - Find missing numbers in a Sequence

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

    Please check below code.....

    function solution(A) {
       var max = Math.max.apply(Math, A);
       if(A.indexOf(1)<0) return 1;
       var t = (max*(max+1)/2) - A.reduce(function(a,b){return a+b});
       return t>0?t:max+1;
    }
    

提交回复
热议问题