题解 | #移除数组中的元素#

移除数组中的元素

https://www.nowcoder.com/practice/a93dd26ebb8c425d844acc17bcce9411

快慢指针
function removeWithoutCopy(arr, item) {
    let fast = 0
    let slow = 0
    while(fast < arr.length) {
        if(arr[fast] !== item) {
            arr[slow] = arr[fast]
            slow++
        }
        fast++
    }
    arr.length = slow
    return arr
}


全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务