C++ Solution with two pointers


  • 0
    Y

    Pointer j increases only for unique element.

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

        set<int> si;
        int j=0;
        for (int i=0; i<nums.size(); i++) {
            if (si.find(nums[i]) == si.end()) {
                si.insert(nums[i]);
                nums[j++] = nums[i];
            }
        }
        return j;
        
    }
    

    };


Log in to reply
 

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