1 <!DOCTYPE html>
2 <html>
3 <head lang="en">
4 <meta charset="UTF-8">
5 <title>@返回字符串中出现次数最多的那个字符和次数2</title>
6
7 </head>
8 <body>
9 </body>
10
11 <script type="text/javascript">
12 function tom(str) {
13 var obj = {};
14 for (var i = 0; i < str.length; i++) {
15 var s = str[i];
16 if (!obj[s]) {
17 obj[s] = 1;
18 } else {
19 obj[s]++;
20 }
21 }
22
23 var num = 0;
24 var word = "";
25
26 for (var item in obj) {
27
28 if (obj[item] > num) {
29 num = obj[item];
30 word = item;
31 }
32 }
33 return ("num:" + num + " word:" + word)
34 }
35 var strs = "aabbbccc";
36 var h = tom(strs);
37 console.log(h);
38 </script>
39 </html>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>@返回字符串中出现次数最多的那个字符和次数2</title>
</head>
<body>
</body>
<script type="text/javascript">
function tom(str) {
var obj = {};
for (var i = 0; i < str.length; i++) {
var s = str[i];
if (!obj[s]) {
obj[s] = 1;
} else {
obj[s]++;
}
}
var num = 0;
var word = "";
for (var item in obj) {
if (obj[item] > num) {
num = obj[item];
word = item;
}
}
return ("num:" + num + " word:" + word)
}
var strs = "aabbbccc";
var h = tom(strs);
console.log(h);
</script>
</html>