26_Remove Duplicates from Sorted Array
28. Remove Duplicates from Sorted Array
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the new length.Solution 1
def removeDuplicates(nums):
"""
:type nums: List[int]
:rtype: int
"""
i = 1
while i < len(nums):
if nums[i] == nums[i-1]:
nums.pop(i)
else:
i += 1
return len(nums)Solution 2
Variant
Last updated