Attempt 1 ❎

'''
gyan pelo
fast and slow pointer will be used
this is similar to last problem just the taboo num will be dynamic
gyan is okay, how to code it?
now the problem is clear, I just need to add one condition which is 
going to decide the taboo num?
if nums[s] == nums[f]:
    taboo_num = nums[s]
    
any edge case for above,
will fast pointer always end after completing whole array?
index out of bound error?
 
slow_pointer, fast_pointer = 0, 0
 
while fast_pointer < len(nums):
    if nums[slow_pointer] == nums[fast_pointer]:
        fast_pointer++
    else:
        slow_pointer++
        fast_pointer++
        nums[slow_pointer] = nums[fast_pointer]
        
    return slow_pointer
 
 
'''
class Solution(object):
    def removeDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        slow_pointer, fast_pointer = 0, 0
 
        while fast_pointer < len(nums):
            cur = nums[fast_pointer]
            if nums[slow_pointer] == nums[fast_pointer]:
                taboo_num = nums[slow_pointer]
                
            if cur == taboo_num:
                fast_pointer += 1
            else:
                nums[slow_pointer] = nums[fast_pointer]
                slow_pointer += 1
                fast_pointer += 1
 
        return slow_pointer
        

Attempt 2 ✅

How can theruntime andmemory_usuage be improved here?

  • In this if you want to replace every second duplicate element then replace +1 with +2.
class Solution:
    def removeDuplicates(self, nums):
        if not nums:
            return 
        slow = fast = 0
        while fast <= len(nums) - 1:
            if nums[fast] != nums[slow]:
                nums[slow+1] = nums[fast]
                slow += 1
            fast += 1
        return slow + 1