DEV Community

Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

Leetcode - 80. Remove Duplicates from Sorted Array II

/**
 * @param {number[]} nums
 * @return {number}
 */
var removeDuplicates = function(nums) {
    let l = 0 , r = 0 ;

    while (r < nums.length){


        let count = 1 ;

        while(r+1<nums.length && nums[r]== nums[r+1]){
            count++;
            r++;
        }


        let times =  Math.min(count,2);

        while(times>0){
            nums[l] = nums[r];
            l++
            times--;
        }

        r = r + 1
    }
    return l;
};
Enter fullscreen mode Exit fullscreen mode

Top comments (0)