Created
April 29, 2013 04:24
-
-
Save cssoul/5479691 to your computer and use it in GitHub Desktop.
获取字符串中出现次数最多的字符以及出现次数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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