int removeDuplicates(vector<int>& nums) {
int pos = 0;
for (int i = 0; i < nums.size(); ++i) {
if (i == 0 || nums[i] != nums[pos - 1])
nums[pos++] = nums[i];
}
return pos;
}

Please note this in the problem: It doesn't matter what you leave beyond the new length.
So we just need to put all the unique elements into nums[0:pos].
It's all right to have duplicate elements in nums[pos:].