Remove Duplicates from Sorted Array

2017/11/30

Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int tmp = INT_MIN;
        int log = 0;
        for (int i =0; i<nums.size(); i++) {
            if (tmp!=nums[i]) {
                tmp = nums[i];
            }else{
                log++;
            }
            nums[i-log] = nums[i];
        }
        
        return nums.size()-log;
    }
};