Skip to content

Instantly share code, notes, and snippets.

@vedal
Last active January 31, 2018 10:31
Show Gist options
  • Save vedal/b7f932256208159988df340f1198ef54 to your computer and use it in GitHub Desktop.
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
// 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