```
public static int removeDuplicates(int[] nums) {
int count=0; //used to save the number of arrays
if(nums==null||nums.length==0) return count;
if(nums.length<=2) return 2;
count=1;
int k=1;//set a tag
for(int i=1;i<nums.length;i++){
if(k<2){ //match the number of deleted
if(nums[i]==nums[i-1]){
nums[count++]=nums[i];
k++;
}else {
//move and restore k value
nums[count++]=nums[i];
k=1;
}
}else {// not match the number of deleted
if(nums[i]!=nums[i-1]){
//move and restore k value
nums[count++]=nums[i];
k=1;
}else {
//not match k ++
k++;
}
}
}
return count;
```