出现最多的字符
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//字符串:let str = '132642343739373635384628633124';
//求这个字符串里面出现最多的字符是哪个,出现了几次
let str = '132642343739373635384628633124'
//新建一个对象 字符做属性,次数做值
let obj = {
}
//遍历对象
for (let i = 0; i < str.length; i++) {
//判断,有这个数就为真,自增。动态属性访问用对象[属性]
if (obj[str[i]]) {
obj[str[i]]++;
} else {
//没有这个数就让他为1
obj[str[i]] = 1;
}
}
//设置一个数等于对象中1出现的次数
let cont = obj[1];
//设置一个数等于对象中1出现的次数
let max = 1;
//遍历
for (let key in obj) {
//判断最大的属性次数,并赋值次数 再赋值属性
if (cont < obj[key]) {
cont = obj[key]
max = key;
}
}
console.log(max)
console.log(cont)
</script>
</body>
</html>