// 方法1: let str = 'zhumolanglafengyouyihah'; let obj={}; [].forEach.call(str,char=>{ if(typeof obj[char]!=='undefined'){ obj[char]++; return; } obj[char]=1; }) let max=1; let res = []; for(let key in obj){ let item = obj[key]; item > max ? max = item : null; } for(let key in obj){ let item = obj[key]; if(item === max){ res.push(key); } } console.log(`出现次数最多的是:${res},出现了${max}`,obj);//出现次数最多的是:h,a,出现了3
//方法2排序 let str = 'zhumolanglafengyouyihah'; str = str.split('').sort((a,b)=>a.localeCompare(b)).join(''); console.log(str); //aaaefgghhhillmnnoouuyyz let reg = /([a-zA-Z])1+/g; let ary=str.match(reg).sort((a,b)=>b.length-a.length); let max = ary[0].length; let res =[ary[0].substr(0,1)] for(let i=1;i<ary.length;i++){ if(ary[i].length < max){ break; } res.push(ary[i].substr(0,1)) } console.log(`出现次数最多的是:${res},出现了${max}`); //出现次数最多的是:a,h,出现了3
//方法3 let str = 'zhumolanglafengyouyihah'; let max = 0, res = [], flag=false; str = str.split('').sort((a,b)=>a.localeCompare(b)).join(''); for(let i = str.length;i>0;i--){ let reg = new RegExp('([a-zA-Z])\1{'+(i-1)+'}','g'); str.replace(reg,(content,$1)=>{ res.push($1); max=i; flag=true; }) if(flag)break; } console.log(`出现次数最多的是:${res},出现了${max}`);//出现次数最多的是:a,h,出现了3