题解 | #查找元素位置#

查找元素位置

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

相关推荐

08-05 18:14
门头沟学院 Java
小花的沉默:是学历厂没错啊,学历太高了不要
投递小鹏汽车等公司10个岗位
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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