Simple commented JavaScript solution


  • 0
    A
    var removeElement = function(nums, val) {
        var i = 0;
        var len = nums.length;
        
        while (i < len) {
            if (nums[i] === val) {
                // remove number from list
                nums.splice(i, 1);
                // adjust list length
                // (could also set len = nums.length)
                len--;
            } else {
                // only increment index if
                // number is not a match
                i++;
            }
        }
        
        return len;
    };
    

Log in to reply
 

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