function getMinStr(str) {
const countObj = {}
for (let i of str) {
Reflect.has(countObj, i) ? countObj[i]++ : countObj[i] = 1
}
const result = Object.entries(countObj).reduce((pre, cur) => cur[1] > pre[1] ? cur : pre)
return result[0]
}
// 使用排序
function getMinStr2(str) {
const countObj = {}
for (let i of str) {
Reflect.has(countObj, i) ? countObj[i].count++ : countObj[i] = (i, { value: i, count: 1 })
}
const result = Object.values(countObj).sort((a, b) => b.count - a.count)[0].value
// return result
}
console.log(getMinStr('wasdjklmaaakdas'));
标签:count,字符,pre,countObj,最少,result,str,字符串,const
From: https://www.cnblogs.com/zx529/p/18022451