获取字符串中出现次数最多的字符。
var str ="abcdefgaddda";
var obj ={};
for (var i = 0, l = str.length; i < l; i++){
var key =str[i];
if (typeof obj[key] == 'undefined') {
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+"出现次数:"+max_key);
网友评论