Skip to content

Latest commit

 

History

History

26-RemoveDuplicatesfromSortedArray

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Remove Duplicates from Sorted Array

Problem can be found in here!

def removeDuplicates(nums: List[int]) -> int:
    slow, fast = 0, 1
    while fast < len(nums):
        if nums[fast] != nums[slow]:
            slow += 1
            nums[slow] = nums[fast]
        fast += 1

    return slow + 1

Explanation: We use a fast pointer to find the next unique value. Then, we switch the value between slow+1 and fast.

Time Complexity: O(n), Space Complexity: O(1)