例子
代码如下 | 复制代码 |
var str = “abcdefgaaaaaaddda”; var obj = {}; var arr = []; var letter; for(var i = 0,len = str.length;i<len;i++){ letter = str[i]; if(!obj[letter]){ obj[letter] = 1; }else{ obj[letter]++; } } var max_key,max_num=0; for(key in obj){ if(max_num <obj[key]){ max_num = obj[key]; max_key = key; } } document.write(“字母:”+max_key+” 次数:”+max_num); |
例子
代码如下 | 复制代码 |
<script> var str = '万wwwaann波波波波波波ndeee博rrrr客'; alert(getMost1(str)); function getMost1(str){ var most = [], num = 0; while( str != '' ){ var ori = str, target = str.substr(0,1), //目标字符 re = target; if(/[$()*+.?]/.test(target)) re = '\' + re; str = str.replace(new RegExp(re, 'g'), ''); diff = ori.length - str.length; //计算目标字符数目 if(diff > num) { num = diff; most = [target]; } else if (diff == num) { most.push(target); } } return num + ': ' + most; } </script> |
时间: 2024-12-27 09:25:11