Remove Duplicates from Sorted Array
If a question mentions a specific property about the input data, often the optimal solution will make use of this property.
In this case, we are told that the array is sorted. That means our optimal solution must make use of the fact it is sorted.
Listen carefully to what your interviewer is telling you, every word might come in handy.
counter = 0
# We use while loop to try and cut down on 2 loops
while counter < (len(nums) - 1):
# if current counter is the same as the next one
# remove current
if nums[counter] == nums[counter + 1]:
nums.remove(nums[counter])
else:
# else they are not the same, so counter +=1
counter += 1
return len(nums)Last updated
Was this helpful?