[
two pointers
]
Leetcode 0026. Remove Duplicates from Sorted Array
Problem statement
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
Solution
It can be done with classical two pointers idea, slow-runner and fast-runner.
Complexity
Time complexity is $O(n)$, space is $O(1)$.
Code
class Solution:
def removeDuplicates(self, nums):
slow, fast = 1, 1
while fast < len(nums):
if nums[slow - 1] != nums[fast]:
nums[slow] = nums[fast]
slow += 1
fast += 1
return slow