Remove Duplicates from Sorted Array
Today I did this Leetcode problem. It was pretty simple. I had to remove all duplicates from an array of integers stored in ascending order. I made two pointers to handle the logic. One to count the number of unique elements and one to iterate through the entire array. Here is my solution:
class Solution: def removeDuplicates(self, nums: List[int]) -> int: count = 1 pointer = 1 while pointer < len(nums): # elements are the same if nums[pointer] == nums[pointer - 1]: pointer += 1 else: nums[count] = nums[pointer] count += 1 pointer += 1 return count