Leetcode Remove Duplicates from Sorted Array II Problem Solution

In this Leetcode Remove Duplicates from Sorted Array II Problem Solution Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

Leetcode Remove Duplicates from Sorted Array II Problem Solution


Problem solution in Python.

class Solution(object):
    def removeDuplicates(self, nums):
        writer, idx = 0, 0
        while idx < len(nums):
            if idx == 0 or nums[idx] != nums[idx - 1]:
                nums[writer] = nums[idx]
                writer += 1
            elif idx == len(nums) - 1 or nums[idx] != nums[idx + 1]:
                nums[writer] = nums[idx]
                writer += 1
            idx += 1
        return writer

Problem solution in Java.

class Solution {
    public int removeDuplicates(int[] nums) {
        int slow = 0;
        int fast = 0;
        int prev = 0;
        int num = 0;
        
        while (fast < nums.length){
            if (fast == 0 || nums[fast] != prev){
                prev = nums[fast];
                num = 1;
            }else if(nums[fast] == prev){
                num++;
            }
            
            if (num <= 2)
                nums[slow++] = nums[fast++];
            else
                fast++;
        }
        
        return slow;
    }
}


Problem solution in C++.

class Solution {
public:
int removeDuplicates(vector& nums) {
int len=0,c=1;
if(nums.size()<=2)
return nums.size();
   vector<int>::iterator it1,it2;
    
    for(int i=1;i<nums.size();i++)
    {
        if(nums[i]==nums[i-1])
        {
            c++;
        }
        else if(nums[i]!=nums[i-1])
        {
            if(c<=2)
                len=len+c;
            else
            {
                it1=nums.begin()+i-c+2;
                it2=nums.begin()+i;
                nums.erase(it1,it2);
                len=len+2;
                
                
                i=i-c+2;
            }
            c=1;
        }
    }
    int length=nums.size();
   
  
    if(nums[length-2]!=nums[length-1])
    {
        len=len+1;
    }
    else
        len=len+2;
    return len;
    
}
 
};


Problem solution in C.

int removeDuplicates(int* nums, int numsSize) {
    int left = 0, right = 1, dup_found = 0;
    
    if (!nums || (numsSize <= 1)) {
        return numsSize;
    }
    while(right < numsSize) {
        if (nums[right] != nums[left]) {
            if ((nums[left+1] == nums[left]) && !dup_found && ((right - left) > 2)) {
                left++;
                dup_found = 1;
            } else {
                dup_found = 0;
            }
            nums[left+1] = nums[right];
            left++;
        } else {
            if (!dup_found) {
                nums[left+1] = nums[right];
                left++;
                dup_found = 1;
            }
        }
        right++;
    }
    return left+1;
}


Post a Comment

0 Comments