Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save priyankvex/05ac65c6e42eb9be19b70acd171530e9 to your computer and use it in GitHub Desktop.
Save priyankvex/05ac65c6e42eb9be19b70acd171530e9 to your computer and use it in GitHub Desktop.
Remove duplicates from a sorted array without extra space
"""
https://scammingthecodinginterview.com
Week 5: Two Pointer
Problem: 1
"""
class Solution(object):
def solve(self, a):
start = 0
current = 1
n = len(a)
while current < n:
if a[current] == a[start]:
current += 1
else:
start += 1
a[start] = a[current]
current += 1
return start + 1
if __name__ == "__main__":
a = [1, 1, 1, 2, 2, 3]
ans = Solution().solve(a)
print(a)
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment