A two-pointer approach to remove duplicates in-place from a sorted array
read) to iterate through the current numbers, and another pointer (compare) to compare with the current number.read obviously starts from 1, because comparing the first number with the previous number is not possible.compare starts from 0.read proceeds by a for loop starting from index 1.nums[read] is different from the previous element nums[compare].read.nums[compare].compare only proceeds when we find a new unique element, ensuring all unique elements are packed at the front of the array and we always have compare <= read.read reaches the end of the array. The value of compare+1 at this point will be the total number of unique elements.class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
compare = 0
for read in range(1, len(nums)):
if nums[read] != nums[compare]:
compare += 1
nums[compare] = nums[read]
return compare+1
read and record).