My 0(n) o(1) solution for general case of k duplicates allowed


  • 0
    I

    int removeDuplicates(int* nums, int numsSize)

    {
    int dupcounter = 0;
    int i, k = 0;
    for (i = 0; i < numsSize;)
    {
    dupcounter = 0;
    do
    {
    if (dupcounter ++ < 2)
    nums[k ++] = nums[i];
    i ++;
    } while (nums[i] == nums[i - 1] && i < numsSize);
    }
    return k;
    }


Log in to reply
 

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