评 论
JS找出字符串中出现最多的字符1
用javascript找出一串字符中出现次数最多的字符。
据说预定义undefined,用while代替for,都可以提高性能,代码如下。
程序代码 JS找出字符串中出现最多的字符
function MaxChar(str)
{
var map={},maxCount=0,maxChar,undefined,i=str.length;
while (i--)
{
var ch = str.charAt(i);
map[ch]==undefined?map[ch]=1:map[ch] += 1;
if (map[ch] > maxCount)
{
maxChar = ch;
maxCount = map[maxChar];
}
}
return [maxChar,maxCount];
}
长篇评论
No comment yet!
短篇评论 <共 0 条评论>
相关主题
没有相关

