Problem with test case [1, 2] (Javascript)


  • 0
    B

    When I test this code locally, my console outputs the correct results [1, 2].

    However, when submitting the same code, this test case fails and says my output is [].

    Can anyone spot the error here? I believe my solution is correct.

    var removeDuplicates = function(nums) {
      if (nums.length == 1)
        return nums;
    
      // keep track of last value
      let last;
      for (let i = nums.length - 1; i > -1; --i) {
        // if current number matches last, remove item
        if (nums[i] === last && nums.length > 1)
          nums.splice(i, 1);
        // set new last number
        last = nums[i];
      }
      
      return nums;
    };
    

Log in to reply
 

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