Skip to main content

80. Remove Duplicates from Sorted Array II

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/

Python

  • Time: O(n)
  • Space: O(logn)
from collections import defaultdict
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
cur, end = 0, len(nums)-1

counter = defaultdict(int)

while cur <= end:
num = nums[cur]
counter[num] += 1

if counter[num] > 2:
nums.pop(cur)
end -= 1
continue
cur += 1

return end+1