```
public class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 1)
return 1;
int length = 0;
for (int i = 0, j = 0; j < nums.length; i++) {
while (nums[i] == nums[j]) {
j++;
if (j == nums.length) {
break;
}
}
if (j != nums.length) {
nums[i + 1] = nums[j];
}
length++;
}
return length;
}
}
```