Skip to content

Instantly share code, notes, and snippets.

@drem-darios
Last active February 9, 2020 07:48
Show Gist options
  • Save drem-darios/e0980a2c44ade574ce4b727b6916740a to your computer and use it in GitHub Desktop.
Save drem-darios/e0980a2c44ade574ce4b727b6916740a to your computer and use it in GitHub Desktop.
Given an array of sorted numbers, remove all duplicates from it. You should not use any extra space; after removing the duplicates in-place return the new length of the array.
def remove_duplicates(arr):
firstPointer = 1
secondPointer = 1
while secondPointer < len(arr):
if arr[firstPointer - 1] != arr[secondPointer]:
# Replace duplicate with non duplicate
arr[firstPointer] = arr[secondPointer]
firstPointer += 1
secondPointer +=1
return firstPointer
def main():
print(remove_duplicates([2, 3, 3, 3, 6, 9, 9]))
print(remove_duplicates([2, 2, 2, 11]))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment