Skip to content

Instantly share code, notes, and snippets.

@cssoul
Created April 29, 2013 04:24
Show Gist options
  • Save cssoul/5479691 to your computer and use it in GitHub Desktop.
Save cssoul/5479691 to your computer and use it in GitHub Desktop.
获取字符串中出现次数最多的字符以及出现次数
var maxchar = function(s){
var o = {},t = [0,''];
for(var i = 0 , l = s.length; i < l ; i++){
if(o[s[i]]){
o[s[i]] += 1;
if(o[s[i]] > t[0]){
t[0] = o[s[i]];
t[1] = s[i];
}
}else{
o[s[i]] = 1;
}
}
return t;
}
console.log(maxchar('abcdefgabcabcefggg'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment