80. Remove Duplicates from Sorted Array II
Remove the duplicates in-place such that each unique element appears atmost twice. The relative order of the elements should be kept the same.
Return the number of unique elements in nums
.
Intuition
We can move an element forward by the amount of duplicates before it.
Approach
We track the occurrence of an element.
Start moving the next elements forward only if the current elements occurrence is greater 2.
Complexity
Time complexity : $\text{O}(n)$
We iterate the array only once
Space complexity : $\text{O}(1)$
We use no extra space, do operations in-place
Code
Another Way
Last updated