83. Remove Duplicates from Sorted Array
🟩 Easy
Question
Given a sorted array nums, remove the duplicates in-place 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 in-place with O(1) extra memory.
Example 1:
Example 2:
Two Pointer Approach
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.
Complexity
Time complexity: O(n)
Space complexity: O(1)
Code
Last updated
Was this helpful?