https://leetcode.com/problems/remove-duplicates-from-sorted-array/
- Two Pointer
Use two pointers: one for the place to write unique elements, the other to scan.
O(n)
O(1)
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int idx = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] != nums[idx]) nums[++idx] = nums[i];
}
return idx + 1;
}
}