int count = 0;
for(int i = 1; i < n; i++){
if(A[i] == A[i1]) count++;
else A[icount] = A[i];
}
return ncount;
Share my clean C++ code

why no one erase the unnecessary elements at the tail?
int Solution::removeDuplicates(vector<int>& nums) { int num_duplicates = 0; for (int i = 1; i < nums.size(); ++i) { if (nums[i] == nums[i  1]) { ++num_duplicates; } else { nums[i  num_duplicates] = nums[i]; } } nums.erase(nums.end()  num_duplicates, nums.end()); // add this line!!! return nums.size(); }



@jasusy
If theTwo identical numbers are not adjacent，such as a[6]=｛1，3，1，4，1，4｝，what is the result?

@Litch said in Share my clean C++ code:
@jasusy
If theTwo identical numbers are not adjacent，such as a[6]=｛1，3，1，4，1，4｝，what is the result?sorry,i don not see"Given a sorted array"

I thought this solution is awesome but it still need to erase unnecessary elements at the tail. For example, if the input is {1,2,2,3,4,5,5,6}, the output is {1,2,3,4,5,6,5,6}. Because the final elements still remain, and the following is my solution.
int removeDuplicates(vector<int>& nums) { int count = 0; int n = nums.size(); vector<int> newNums; for (int i = 1; i < n; i++) { if (nums[i] == nums[i  1]) count++; else nums[i  count] = nums[i]; } for (int i = 0; i < n  count; i++) { newNums.push_back(nums[i]); } //return newNums; return n  count; }
