83. Remove Duplicates from Sorted Array
🟩 Easy
Last updated
Was this helpful?
🟩 Easy
Last updated
Was this helpful?
Given a sorted array nums, remove the duplicates such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array with O(1) extra memory.
Example 1:
Example 2:
Two Pointers: i for distinct element, k for searching.
If nums[k] is duplicate, put in i+1(since k goes faster than i, so k > i). Therefore, nums[0... i] are the distinct elements in original array.
Time complexity: O(n)
Space complexity: O(1)