Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
- Change the array
numssuch that the firstkelements ofnumscontain the unique elements in the order they were present innumsinitially. The remaining elements ofnumsare not important as well as the size ofnums. - Return
k.
solution
Because we don’t care about what ends up at the end of the array, just overwrite values.
def removeDuplicates(self, nums: List[int]) -> int:
res = len(nums)
next_insert = 0
i = 0
while i < len(nums):
# put next unique value at next avail idx
nums[next_insert] = nums[i]
next_insert += 1
# advance i to next unique value
i += 1
while i < len(nums) and nums[i] == nums[i-1]:
i += 1
res -= 1
return res