Javascript O(N)


  • 0
    E

    I get an error because it says [1,1] should return [2] but I think it is a mistake. I have tested it in many cases.

    var findDisappearedNumbers = function(nums) {
      var min = Math.min(...nums);
      var max = Math.max(...nums);
      var numsHash = {};
      var missingNums = [];
      
      nums.forEach(function(num){
        numsHash[num]=true;
      });
      
      var len = Object.keys(numsHash).length
      var dif = max-min
    
      for(var i=0; i < dif; i++){
        if(numsHash[min] === undefined){
          missingNums.push(min)
          min++
        }else{
          min++
        }
      }
      return missingNums
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.