题解 | #查找元素位置#

查找元素位置

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'))
全部评论

相关推荐

09-22 15:45
门头沟学院 Java
谁给娃offer我给...:我也遇到了,我说只要我通过面试我就去,实际上我根本就不会去😁
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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