题解 | #查找元素位置#

查找元素位置

http://www.nowcoder.com/practice/0a9af9cb20c34babb6232126e019c74d

// 描述
// 在数组 arr 中,查找值与 item 相等的元素出现的所有位置
// 示例1
// 输入:
// ['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c'] 'a'
// 复制
// 输出:
// [0, 6]

function findAllOccurrences(arr, target) {
    let result = []
    arr.forEach((value, index) => {
        if (value == target)
            result.push(index)
    })
    return result
}

console.log(findAllOccurrences(['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c'], 'a'))
全部评论

相关推荐

萧索X:写篮球联赛干嘛,陪老板打篮球吗。还有实习经历要写自己所在岗位具体完成什么工作,自己的任务具体完成了什么需求,给公司带来了哪些量化增长
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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