var str = 'addddbcsaertdd';
function maxCount(str){
var obj = {};
for(var i = 0 ; i < str.length; i++){
var key = str[i];
if(obj[key]){
obj[key]++
}else {
obj[key] = 1;
}
}
var maxCount = 0 ;
var maxString = "";
for(var key in obj){
if(maxCount < obj[key]){
maxCount = obj[key];
maxString = key;
}
}
return '出现最多的字母是:'+ maxString +'出现了'+ maxCount +'次';
}
var a = maxCount(str);
console.log(a);
如果是数组的话 也可以用这个方法,但是前提是要把数组用join(‘’)方法转换成字符串然后传入maxCount 函数。
var arr = [1,2,3,4,5,6,3,2,4];
var newStr = arr.join('');
console.log(newStr); //123456324