Remove duplicates from sorted array II

August 29, 2022

array-and-hashmap

Problem URL: Remove duplicates from sorted array II

We will iterate over the whole array from index 1 to the end-1, compare the values of 3 consecutive values, if they are same, we change the first element to 10001, as our input can be as big as 10^4. Then we sort them and count the number of element which are 10001, and return the count.

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        for i in range(1, len(nums)-1):
            if nums[i-1] == nums[i] == nums[i+1]:
                nums[i-1] = 10001
        nums.sort()

        count = 0 
        for n in nums:
            if n != 10001:
                count += 1
        return count

Time Complexity: O(nlog(n))
Space Complexity: O(1)