Last active
January 31, 2018 10:31
-
-
Save vedal/b7f932256208159988df340f1198ef54 to your computer and use it in GitHub Desktop.
[Remove duplicates in-place] loop over list and remove duplicates with O(1) space complexity #list #duplicates #inplace
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
// vim: syntax=python | |
def removeDuplicates(nums): | |
""" | |
:type nums: List[int] | |
:rtype: int | |
""" | |
if len(nums) < 2: | |
return len(nums) | |
prev = nums[0] | |
i = 1 | |
while i < len(nums): | |
if nums[i] == prev: | |
del[nums[i]] | |
else: | |
prev = nums[i] | |
i += 1 | |
return len(nums) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment