美文网首页
字符出现次数最多

字符出现次数最多

作者: peerben | 来源:发表于2019-04-19 11:28 被阅读0次

随手练习, 代码简化后舒服多了

判断一个字符串中出现次数最多的字符,统计这个次数。

function alphaStatis(string: string) {
  const map = new Map();

  Array.from(string).forEach(key => {
    const count = map.get(key) || 0;
    map.set(key, count + 1);
  });

  return Array.from(map.entries()).reduce((max, cur) => {
    return max[1] > cur[1] ? max : cur;
  });
}

const res = alphaStatis('abcdefgaddda');
console.log(`max ${JSON.stringify(res)}`);

相关文章

网友评论

      本文标题:字符出现次数最多

      本文链接:https://www.haomeiwen.com/subject/bohqgqtx.html