```
public int removeDuplicates(int[] nums) {
int count = 0;
int i = 0 ;
while( i < nums.length ){
if(i + 1 < nums.length && nums[i] == nums[i+1]){
nums[count++] = nums[i];
nums[count++] = nums[i];
int k = nums[i];
i = i + 2;
while(i < nums.length && k == nums[i]){
i++;
}
}else{
nums[count++] = nums[i];
i++;
}
}
return count;
}
```