Problem statement

https://binarysearch.com/problems/Remove-Duplicates-Occurring-More-Than-Twice/

Solution

Equal to Leetcode 0080. Remove Duplicates from Sorted Array II.

Complexity

It is O(n) for time and O(1) for space.

Code

class Solution:
    def solve(self, nums):
        if len(nums) < 2: return nums
        slow, fast = 2, 2

        while fast < len(nums):
            if nums[slow - 2] != nums[fast]:
                nums[slow] = nums[fast]
                slow += 1
            fast += 1
        return nums[:slow]