<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>js出现次数最多的是</title>
    <link rel="stylesheet" href="">
    <script>
        var str='asdhdsaaaadasds';
        var json={};

        for (var i = 0; i < str.length; i++) {
                if(!json[str.charAt(i)]){
                        json[str.charAt(i)] = 1;
                }else{
                        json[str.charAt(i)]++;
                }
        };
        var iMax = 0;
        var iIndex = '';
        for(var i in json){
                if(json[i]>iMax){
                        iMax = json[i];
                        iIndex = i;
                }
        }
        alert('出现次数最多的是:'+iIndex+'出现'+iMax+'次');
    </script>
</head>
<body>
    asdhdsaaaadasds
</body>
</html>

标签:JavaScript

你的评论