Last active
February 9, 2020 07:48
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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