diff --git a/0080-remove-duplicates-from-sorted-array-ii/0080-remove-duplicates-from-sorted-array-ii.py b/0080-remove-duplicates-from-sorted-array-ii/0080-remove-duplicates-from-sorted-array-ii.py new file mode 100644 index 0000000..29ea220 --- /dev/null +++ b/0080-remove-duplicates-from-sorted-array-ii/0080-remove-duplicates-from-sorted-array-ii.py @@ -0,0 +1,23 @@ +class Solution: + def removeDuplicates(self, nums: List[int]) -> int: + readIndex = 1 + writeIndex = 1 + + lastValueSeen = nums[0] + numTimesValueSeen = 1 + while readIndex < len(nums): + if nums[readIndex] == lastValueSeen: + numTimesValueSeen += 1 + + if numTimesValueSeen <= 2: + nums[writeIndex] = nums[readIndex] + writeIndex += 1 + else: + lastValueSeen = nums[readIndex] + numTimesValueSeen = 1 + nums[writeIndex] = nums[readIndex] + writeIndex += 1 + + readIndex += 1 + + return writeIndex \ No newline at end of file