javascript 實現 統計字符串內相同字符個數

function calculate(str){
     var map={};
     var maxCount=0;   //統計最大值
     //遍歷字符串
     for(var i=0;i<str.length;i++){
         if(typeof map[str[i]]=="undefined")  //屬性值未定義
              map[str[i]]=1;
         else //若是屬性值已經定義
             map[str[i]]+=1;
         if(maxCount<map[str[i]])
            maxCount=map[str[i]];
     }
     return maxCount;
 }
相關文章
相關標籤/搜索