Simple C++ solution


  • 0
    T
    class Solution {
    public:
        int removeDuplicates(vector<int>& nums) {
            int n = nums.size();
            if (n <= 1) return n;
    
            int length = 1;
            for (int i = 1; i < n; i++) {
                if(nums[i] != nums[i-1]) {
                    nums[length++] = nums[i];
                }
            }
            return length;
        }
    };
    

Log in to reply
 

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