```
# @param {Integer[]} nums
# @return {Integer}
def remove_duplicates(nums)
non_dupe_index = 0
nums.each do |num|
if (non_dupe_index == 0 || num > nums[non_dupe_index - 1])
nums[non_dupe_index] = num
non_dupe_index += 1
end
end
nums.replace(nums)
non_dupe_index
end
```

This solution runs at 109 ms and beats 40.91%.

Here is a faster solution that may stretch the constant memory restraint a bit;

```
# @param {Integer[]} nums
# @return {Integer}
def remove_duplicates(nums)
last = nil
(0...nums.size).each do |i|
num = nums[i]
if last == num
nums[i] = nil
else
last = num
end
end
nums.replace(nums.compact)
nums.size
end
```

This solution runs at 82ms and beats 86.36%.

Basically `.compact`

is super fast. But it's technical creating another array.

Can we just do that in place?

If we replace `nums.replace(nums.compact)`

with `nums.compact!`

it works and makes more sense, but performance drops to 95ms, which only beats 54.55%. Odd to say the least considering that we are basically doing the same thing.