1 var str = 'sdedadedsjojkmihuuhjklnmqwerty'
2 //定义一个对象,存储每个遍历的字符串和出现的次数;
3 var obj = {}
4 for (var i = 0; i < str.length; i++) {
5 //将每次循环,给char赋值,
6 var char = str.charAt(i)
7 //判断obj对象中是否有这字符,obj[char]如果没有,undefined,执行else
8 if (obj[char]) {
9 //如果有这个对象,则将字符串所对应的次数加1;
10 obj[char]++
11 } else {
12 //给这个对象赋值,代表出现了一次
13 obj[char] = 1
14 }
15 }
16 console.log(obj)
1 //定义一个最大值,遍历对象,获取次数最多的属性名和次数
2 var max = 0
3 //变量ch用来存储最后每次遍历对象他的属性名key;
4 var ch = ''
5 for (var k in obj) {
6 if (max < obj[k]) {
7 max = obj[k]
8 ch = k
9 }
10 }
11 console.log(max + '最大值是字符:' + ch)
时间如白驹过隙,忽然而已,且行且珍惜......