Simple JavaScript Solution


  • 0
    Z

    Stepping through the area once, splicing off duplicates:

    /**
     * @param {number[]} nums
     * @return {number}
     */
    var removeDuplicates = function(nums) {
        if(!nums)
            return null;
        
        if(nums.length === 1)
            return 1;
    
        for (var i = 1; i < nums.length; i++){
            if(nums[i] === nums[i-1]){
                nums.splice(i, 1);
                i--;
            }
        }
        return nums.length;
    };
    

Log in to reply
 

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