My simple C++ solution. O(n) time and O(1) space, maintaining the original order


  • 0
    M

    class Solution {
    public:
    int removeDuplicates(vector<int>& nums) {

        if(nums.size() == 0 || nums.size() == 1)
            return nums.size();
        
        int n = nums[0];
        int count = 1;
        
        for(int i=1;i<nums.size();i++)
        {
            if(nums[i] != n)
            {
                nums[count]=nums[i];
                n=nums[i];
                count++;
            }
        }
        
        return count;
    }
    

    };


Log in to reply
 

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