直接上代码:
function findMaxCountStr(str) {
const arr = str.split('')
const obj = {}
let result = {}
arr.forEach(e => {
obj[e] = { key: e, count: obj[e] ? obj[e].count : 0 }
if (Object.keys(obj).indexOf(e) > -1) {
obj[e].count = !obj[e].count ? 1 : (obj[e].count + 1)
} else {
obj[e].count = 0
}
})
const counts = Object.values(obj).map(e => e.count)
const max = Math.max.call(null, ...counts)
for (let i in obj) {
if (obj[i].count === max) {
result = obj[i]
}
}
return result
}