方法如下:
var str = "abcdefgaddda";
//将字符串转化成对象
var obj = {};
for (var i = 0,l = str.length; i < l; i++){
var key = str[i];
if (!obj[key]) {
obj[key] = 1;
}
else {
obj[key]++;
}
}
//遍历该对象,找出出现次数最多的字符和出现次数
var max = -1;
var max_key = "";
var key;
for (key in obj) {
if (max < obj[key]) {
max = obj[key];
max_key = key;
}
}
alert("出现最多次数的字符:"+max_key+" 出现次数:"+max);