link: https://leetcode.com/problems/remove-duplicates-from-sorted-array/
Solution: two pointers
class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
// 1, 1, 2
int cnt = 1;
int left = 0, right = 0;
while(right < nums.length) {
if(nums[left] == nums[right]) {
right++;
} else {
left++;
nums[left] = nums[right];
right++;
cnt++;
}
}
return cnt;
}
}
TC: O(n)
SC: O(1)