// 找出⼀个字符串中出现次数最多的字符,并统计出现的次数 // "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}
标签:count,字符,obj,item,res,char,次数,str,字符串 From: https://www.cnblogs.com/gagaran/p/17137626.html