Problem Statement
Pattern:
Solution
public int removeDuplicates(int[] nums) {
int i = 0, j = 0;
while (j < nums.length) {
int seek = j;
while(seek+1 < nums.length && nums[seek+1] == nums[seek]) seek++;
if(seek - j + 1 >= 2) {
nums[i++] = nums[j];
nums[i++] = nums[j];
} else nums[i++] = nums[j];
j = seek + 1;
}
return i;
}
TC : SC :
Notes
- have a third pointer seek forwards to from the second pointer, if it reaches a distance >= 2, then override the next 2 elements, else just a single element