Javascript 120ms


  • 0
    M

    var removeElement = function(nums, val) {

    //two pointer, in-place, one pass
    var begin = 0, end = nums.length-1;
    while (begin <= end) {
        if (nums[begin] === val) {
            nums[begin] = nums[end];
            end--;
        } else {
            begin++;
        }
    }
    return begin;
    

    };


Log in to reply
 

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