My simple javascript solution


  • 1
    K
     var removeDuplicates = function(nums) {
    for(var i=0;i<nums.length;)
    {
        if(nums[i+1]=== nums[i])nums.splice(i, 1);
        else i++;
    }
    return i;
    };

  • 0
    D

    I was doing the same thing as you but I think the point of this problem was to not use splice since it is slow and do something like tusizi's answer:

    var removeDuplicates = function (nums) {
    var i = 0;
    nums.forEach(function (elem) {
    if (elem !== nums[i]) {
    nums[++i] = elem;
    }
    });
    return nums.length && i + 1;
    };


Log in to reply
 

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