function isMax (STR) { let obj = {}; let arr = []; for (let item of STR) { if (!obj[item]) { obj[item] = 1; } else { obj[item]++; }; }; Object.keys(obj).forEach((item, index) => { let res = { name: item, value: obj[item] }; arr.push(res); }); let sort = arr.sort((a, b) => { return b.value - a.value; }); let maxNum = sort[0].value; let maxStr = sort[0].name; console.log(`最多的字元是${maxStr},他有${maxNum}個`); }