let text = 'qqqwwqqwetywtewyqteyweq'
console.log(getMost(text))
function getMost(str){
if(!str || typeof str !== 'string'){
return '请输入字符串'
}
let obj = {};
for(let i=0; i<str.length; i++){
if(obj[str[i]]){
obj[str[i]] += 1;
} else {
obj[str[i]] = 1;
}
}
let num = 0;
let name = '';
for(let key in obj){
if(obj.hasOwnProperty(key)){
if(obj[key] > num){
num = obj[key];
name = key;
}
}
}
return `出现次数最多的是${name},一共出现${num}次`
}
09-30
180
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-18
599
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-09