[Algorithm] Max Chars Problem
2024-09-04 22:14:08
// --- Directions
// Given a string, return the character that is most
// commonly used in the string.
// --- Examples
// maxChar("abcccccccd") === "c"
// maxChar("apple 1231111") === "1" function maxChar(str) {
let m = {},
max = -1,
result = null; for (let char of str) {
m[char] = m[char] + 1 || 1;
} for (let [key, count] of Object.entries(m)) {
max = Math.max(max, count);
if (max === count) {
result = key;
}
} return result;
} module.exports = maxChar;
const maxChar = require('./index'); test('maxChar function exists', () => {
expect(typeof maxChar).toEqual('function');
}); test('Finds the most frequently used char', () => {
expect(maxChar('a')).toEqual('a');
expect(maxChar('abcdefghijklmnaaaaa')).toEqual('a');
}); test('Works with numbers in the string', () => {
expect(maxChar('ab1c1d1e1f1g1')).toEqual('1');
});
最新文章
- 用eclipse开发和调试postgresql-8.4.1
- Note_Master-Detail Application(iOS template)_05_ YJYMasterViewController.m
- NFC(5)编写NFC程序的基本步骤
- Android集成科大讯飞SDK语音听写及语音合成功能实现
- 高级UIKit-08(TCPSocket)
- keystore 介绍
- 进程与进程描写叙述符(task_struct)
- 如何在MQ中实现支持任意延迟的消息?
- mybatis批量提交
- Mac软件安装提示程序已损坏解决方案
- Mybatis集成Oracle
- Quartz.NET 任务调度教程。
- 山寨版 WP8.1 Cortana 启动 PC
- Image Restoration[Deep Image Prior]
- python中lambda表达式中自由变量的坑,因为for循环结束了 变量还保存着,详见关于for循环的随笔
- java BASE64流 输出图片。
- 基于Confluent.Kafka实现的KafkaConsumer消费者类和KafkaProducer消息生产者类型
- URAL 1741 Communication Fiend
- OC 属性关键字
- 关于Linux DNS部分处理