Leetcode 80 Solution

This article provides solution to leetcode question 80 (remove-duplicates-from-sorted-array-ii)

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii

Solution

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int l = 0;
        int r = 0;
        int cnt = 0;

        while (r < nums.size())
        {
            if (r + 1 < nums.size() && nums[r] == nums[r + 1])
                cnt++;
            else
                cnt = 0;

            if (cnt < 2)
                swap(nums[l++], nums[r++]);
            else
                r++;
        }

        return l;
    }
};