JavaScript O(n) and without new array solution


  • 0
    B
    var removeElement = function(nums, val) {
        var newLength = nums.length;
        
        var i = 0;
        while(i < newLength) {
            if (nums[i] === val) {
                newLength--;
                
                nums[i] = nums[newLength];
            }else {
                i ++
            }
        }
    
        return newLength;
    };

Log in to reply
 

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