Mardanjan / Blog

学习笔记(在issues里),一些小demo的源码在这里,demo在线地址会持续更新
1 stars 0 forks source link

算法:统计一个字符串或数组中某个字符或元素的出现次数 #23

Open Mardanjan opened 4 years ago

Mardanjan commented 4 years ago
var str = '大家好啊我是张家辉啊是哪个系兄弟来看我的张家辉啊啊啊啊'
var arr = str.split('')
qwe(arr)
var result = arr.reduce(function(acc, cur) {
    if (cur in acc) {
        acc[cur] ++
    } else {
        acc[cur] = 1
    }
    return acc
}, {})
qwe(result)