判断⼀个字符串中出现次数最多的字符,统计这个次数
// 找出⼀个字符串中出现次数最多的字符,并统计出现的次数
// "ababajshbaasdaaaa" => {char:'a',count:9}
var str = "ababajshbaasdaaaa" function computeString(str) { let obj = {}, res = { char: '', count: 0 } for (let i = 0, len = str.length; i < len; i++) { const item = str[i] obj[item] = obj[item] ? obj[item] + 1 : 1 if (obj[item] > res.count) { res.char = item res.count = obj[item] } } return res } let val = computeString(str) console.log(val); // {char: 'a', count: 9}