代码:
let a='hhhhweeowiohhhlbbccdd';
a=a.split('');
// console.log(a instanceof Array);
function findRes(){
let result=[];
let max=0;
let str='';
//该循环是为了找到各个字符在字符串中出现的的次数
for(let i=0;i<a.length;i++){
let count=1;
for(let j=i+1;j<a.length;j++){
if(a[i]===a[j]){
a.splice(j,1);
j--;
count++;
}
}
result.push(a[i]+":"+count);
}
console.log(result);
//["h:7", "w:2", "e:2", "o:2", "i:1", "l:1", "b:2", "c:2", "d:2"]
//该循环是为了在上述得到的结果中找到出现频率最高的那个字符
for(let i=0;i<result.length;i++){
let num=Number(result[i].charAt(2));
if(max<num){
max=num;
str=result[i].charAt(0);
}
}
console.log(str+":"+max);
}
findRes();
运行结果: