js : 获取字符串"aabbbccc"中出现次数最多的字符

var str="abbccc";
var arr=str.split("");
var map =new Map();
arr.forEach(function(ele,index){
    if(map.has(ele)==false){
        map.set(ele,1);
    }else{
        var num=map.get(ele)+1;
        map.set(ele,num);
    }
});

var max=map.get("a");
for (var [key, value] of map.entries()) {
	if(value>max){
		max=value;
	}
}
console.log("出现次数最多的字符是:"+key + ",有" + value+"个!");
发布了107 篇原创文章 · 获赞 33 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_36069339/article/details/100083717