Simple c++ solution using 2 pointer


  • 0
    W
    int removeDuplicates(vector<int>& nums) {
        
        if (nums.size() < 2) return nums.size();
        
        int current = 0;
        int prev = 0;
        
        while (current < nums.size())
        {
            int next = current + 1;
            while (next < nums.size() && nums[next] == nums[current])
            {
                next++;
            }
            prev++;
            current = next;
            nums[prev] = nums[current];
        }
        
        return prev;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.