统计字符出现的次数并去重

 var str = "aabbccehgfhaasdhgfashdfhabcasd";

    // 使结果显示为一个对象,如:{a:2, b:1, c:2, d:1}

    var obj = {};

    // console.log(obj[ "a" ])     // undefined

    // obj[ "a" ] = 1
    // obj[ "a" ] ++

    // obj[ "b" ] = 1
    // obj[ "b" ] ++
    // obj[ "b" ] ++
    // obj[ "b" ] ++

    // obj[ "c" ] = 1

    for(var i=0;i<str.length;i++){
        // obj[ str[i] ]
        if(obj[ str[i] ]){
            obj[ str[i] ]++;
        }else{
            obj[ str[i] ] = 1;
        }
    }
    console.log(obj);


    var newStr = "";
    for(var key in obj){
        newStr += key;
    }
    console.log(newStr);
原文地址:https://www.cnblogs.com/lhx5213/p/12845002.html