JavaScript solution, O(n) time O(1) space, beats 80%


  • 0
    I
    var removeDuplicates = function (nums) {
        let numsLen = nums.length, index = 1;
        if (numsLen < 2) return nums;
        for (let i = 1; i < numsLen; i++) {
            if (nums[i] !== nums[i - 1]) {
                nums[index] = nums[i];
                index++;
            }
        }
        return index;
    };
    

Log in to reply
 

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