獲取字符串中出現(xiàn)次數(shù)最多的字符浮梢。
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+"出現(xiàn)次數(shù):"+max_key);