查詢字串中出現次數最多的字母和出現的次數. 示例: var str =" fdadffddfdffj"; ====> 出現次數最多的是f, 出現了 6 次;
阿新 • • 發佈:2019-01-09
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
var a = 'abcdeeffffffjjjjjjjjjjj';
var b = {};
for (var i = 0;i < a.length;i++){
if (!b[a[i]]) {
b[a[i]] = 1;
}else {
b[a[i]] = b[a[i]] + 1;
}console.log(b[a[i]]);
}
var s = '';
var iNum = 0;
for (var name in b){
if (iNum < b[name]) {
iNum = b[name];
s = name;
}
}
alert('出現次數最多的字母是:' + s + ',出現了' + iNum + '次.');
</script>
</head>
<body>
</body>
</html>