Last active
April 13, 2021 05:35
-
-
Save KingAshiru/80f4d8682d8c762ec5e73e04ea1c2ff8 to your computer and use it in GitHub Desktop.
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
class Solution: | |
def removeDuplicates(self, nums): | |
#check for cases of an empty array | |
if len(nums) == 0: | |
return 0 | |
nextNonDuplicate = 1 | |
i = 1 | |
#since the array is sorted, all duplicates would be adjacent, | |
#so we start from the first element and check if the next element isn't a duplicate | |
#if it isn't, we update the array in-place using the index | |
while i < len(nums): | |
if nums[nextNonDuplicate - 1] != nums[i]: | |
nums[nextNonDuplicate] = nums[i] | |
nextNonDuplicate += 1 | |
i += 1 | |
return nextNonDuplicate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome and thanks for walking through the feedback @KingAshiru.
See you on Friday!