```
public int removeDuplicates(int[] nums) {
if (nums.length <= 2) {
return nums.length;
}
int length = nums.length;
int start = 0, times = 1;
for (int i = 1; i < length; i++) {
if (nums[i] != nums[start]) {
start++;
nums[start] = nums[i];
times = 1;
} else {
times++;
if (times == 2) {
start++;
nums[start] = nums[start - 1];
}
}
}
return start + 1;
}
```

count how many times the nums[start] comes, if it has 2 times, start++ and set the second one the same as the first one. If the next value is not the same as nums[start], set the next value to nums[start++];