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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay