26 Remove Duplicates from Sorted Array
Given a sorted array, 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 in place with constant memory.
For example,
Naive Approach
The Idea: Erase the element if an adjacent duplicate is found from the array.
Complexity: O(n^2) time and O(1) space
Optimal Approach
The Idea: Add every unique occurrence in the array to the start, and ignore duplicates. That way the start of the array will only populate with unique elements.
Complexity: O(n) time and O(1) space
Last updated