Same simple solution written in several languages. Just go through the numbers and include those in the result that haven't been included twice already.

**C++**

```
int removeDuplicates(vector<int>& nums) {
int i = 0;
for (int n : nums)
if (i < 2 || n > nums[i-2])
nums[i++] = n;
return i;
}
```

**Java**

```
public int removeDuplicates(int[] nums) {
int i = 0;
for (int n : nums)
if (i < 2 || n > nums[i-2])
nums[i++] = n;
return i;
}
```

**Python**

```
def removeDuplicates(self, nums):
i = 0
for n in nums:
if i < 2 or n > nums[i-2]:
nums[i] = n
i += 1
return i
```

**Ruby**

```
def remove_duplicates(nums)
i = 0
nums.each { |n| nums[(i+=1)-1] = n if i < 2 || n > nums[i-2] }
i
end
```