Javascript solution with two pointers


  • 0
    R
    var removeElement = function(nums, val) {
        var endP = nums.length - 1,
            firstP = 0;
        
        while(firstP < endP){
            if(nums[firstP] === val){
                nums[firstP] = nums[endP];
                nums[endP] = val;
                endP--;
            }else{
                firstP++;
            }
        }
        
        if(nums[endP] === val)
            return endP;
        return endP + 1;
    };

Log in to reply
 

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