题解 | #移除数组中的元素# 的两种方案

移除数组中的元素

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

用递归

function removeWithoutCopy(arr, item) {
    let index = arr.findIndex(cur => cur === item)
    if(index < 0) return arr
    arr.splice(index, 1)
    return removeWithoutCopy(arr, item)
}

用死循环(迭代)

function removeWithoutCopy(arr, item) {
    let index = arr.findIndex(cur => cur === item)
    while(index >= 0){
        if(index < 0) return arr
        arr.splice(index, 1)
        index = arr.findIndex(cur => cur === item)
    }
    return arr
}
全部评论

相关推荐

搞机墨镜猫:科研和竞赛全写成项目经历,另外你项目涉及到的技术栈太杂了,应该对不同岗位强调写不同的技术栈,寒假应该不太好找短期,长期明年3,4月好找很多
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务