function getMaxCount1(str) {
let json = {};
let result = '';
for(let i = 0; i < str.length; i++) {
let char = str[i];
if (!json[char]) {
json[char] = 1
}else {
json[char]++;
}
}
var maxCountChar = '';
var maxCount = 0;
for(var key in json) {
if(json[key] > maxCount) {
maxCount = json[key];
maxCountChar = key;
}
}
return `出现最多的值是${maxCountChar}出现最多的次数为${maxCount}`;
}
console.log(getMaxCount1('sdsfsdsdsss'));
function getMaxCount2(str) {
var json = {};
var maxCount = 0, maxCountChar = '';
str.split('').forEach(item => {
if (!json.hasOwnProperty(item)){
var number = str.split(item).length - 1;
if(number > maxCount) {
json[item] = number;
maxCount = number;
maxCountChar = item;
}
}
})
return `出现最多的值是${maxCountChar}出现最多的次数为${maxCount}`;
}
console.log(getMaxCount2('adasfdsfds'))
console