Advanced-Frontend / Daily-Interview-Question

我是依扬(木易杨),公众号「高级前端进阶」作者,每天搞定一道前端大厂面试题,祝大家天天进步,一年后会看到不一样的自己。
https://muyiy.cn/question/
27.43k stars 3.29k forks source link

我也来试试 #590

Open DingSJi opened 3 months ago

DingSJi commented 3 months ago

const fn = function(str){ let arr = str.split("") let obj = {} let maxStr = "", maxNum = 0; arr.reduce((pre, next) => { if(pre in obj){ if(pre == next) obj[pre]++ }else{ obj[pre] = 1 } return next },arr[0]) console.log(obj) for(let key in obj){ if(maxNum < obj[key]){ maxNum = obj[key] maxStr = key } } return ${maxStr}-${maxNum} }

console.log(fn('aaaaaabbbba'))