```
public class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null || nums.length<3){
return nums==null?0:nums.length;
}
int dif = 2;
for(int i=2;i<nums.length;i++){
if(nums[i]!=nums[dif-2]){
nums[dif++]=nums[i];
}
}
return dif;
}
```

}

if N duplicates are allowed,you just need to change the nums[dif-2] to nums[dif-N] ,welcome to discuss with me .