```
public class Solution {
public int RemoveDuplicates(int[] nums) {
int k = 0;
int len = nums.Length;
int max = Int32.MaxValue;
if (len<=2) return len;
for (int i=1; i< nums.Length; i++) {
if (nums[i]==nums[i-1]) {
k++;
if (k > 1) {
nums[i-1] = max;
len--;
}
}
else
k = 0;
}
Array.Sort(nums);
return len;
}
}
```