JavaScript:I get the right output. But why Input:[1,1,1,2] Output:[1,1,1] Expected:[1,1,2] on OJ?


  • 0
    F

    /**

    • @param {number[]} A
    • @return {number}
      */
      var removeDuplicates = function(A) {
      var result=[];
      var arr=[];
      for(var i=0;i< A.length;i++)
      {
      arr[A[i]]===undefined?arr[A[i]]=1:arr[A[i]]++;
      arr[A[i]]<=2&&result.push(A[i]);
      }
      return result.length;
      };

  • 1
    L
    var removeDuplicates = function(nums) {
       if(nums.length < 3) return nums.length;
    
        for(var i = 2; i < nums.length; i++){
            if(nums[i] === nums[i-1] && nums[i] === nums[i-2]) nums.splice(i--, 1);
        }
    
        return nums.length;
    };

Log in to reply
 

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