// --- 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');
});

  

[Algorithm] Max Chars Problem的更多相关文章

  1. HDU 2993 MAX Average Problem dp斜率优化

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  2. 数据结构:HDU 2993 MAX Average Problem

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. MAX Average Problem(斜率优化dp)

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. hdu 2993 MAX Average Problem(斜率DP入门题)

    题目链接:hdu 2993 MAX Average Problem 题意: 给一个长度为 n 的序列,找出长度 >= k 的平均值最大的连续子序列. 题解: 这题是论文的原题,请参照2004集训 ...

  5. BNUOJ 3958 MAX Average Problem

    MAX Average Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Jav ...

  6. HDU 2993 MAX Average Problem(斜率优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 Problem Description Consider a simple sequence w ...

  7. HDU 2993 MAX Average Problem(斜率优化DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给定一个长度为n(最长为10^5)的正整数序列,求出连续的最短为k的子序列平均值的最大 ...

  8. hdu2993 MAX Average Problem (斜率dp)

    参考:http://www.cnblogs.com/kuangbin/archive/2012/08/27/2657878.html //#pragma warning (disable: 4786) ...

  9. HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给出n,k,给定一个长度为n的序列,从其中找连续的长度大于等于k的子序列使得子序列中的 ...

随机推荐

  1. Oracle存储过程——日常记录

    代码规范 Oracle存储过程,关键字大写,变量小写并以v_开头,规范代码,提高可读性 赋值与判断符号 Oracle存储过程,变量赋值使用 := 符号,条件判断直接用 = 符号. 变量声明需在 beg ...

  2. ros 配置udev

    显示已经链接设备 lsusb 显示挂载点 ls /dev/ttyACM* /dev/ttyUSB* 可以看到 ttyUSB0 和 ttyUSB1 对应哪一个设备不确定,因此,我们就需要一种方法来保证每 ...

  3. Linux (x86) Exploit 开发系列教程之四(使用return-to-libc绕过NX bit)

    (1)原理: “NX Bit”的漏洞缓解:使某些内存区域不可执行,并使可执行区域不可写.示例:使数据,堆栈和堆段不可执行,而代码段不可写. 在NX bit打开的情况下,基于堆栈的缓冲区溢出的经典方法将 ...

  4. 升级CentOS 7.4内核版本--升级到最新

    在实验环境下,已安装了最新的CentOS 7.4操作系统,现在需要升级内核版本.实验环境 CentOS-7-x86_64-Minimal-1708.isoCentOS Linux release 7. ...

  5. nginx反向代理服务器以及负载均衡,从安装到配置

    nginx的具体作用不用细说,很强大,做负载均衡.反向代理服务器解决前端跨域问题等等.下面是nginx的安装过程 首先nginx主要的依赖: pcre. pcre-devel zlib zlib-de ...

  6. 出现 HTTP 错误 500.19 错误代码 0x800700b7

    这个内容出现主要问题是在IIS上,我们一般程序开发 iis中默认的路径只是http://localhost/,相当于环境变量中已定义好了,如果自己创建的项目直接将路径定义到这,就会替换图二中的路径,然 ...

  7. NetScaler Logs Collection Guide

    NetScaler Logs Collection Guide 来源  https://support.citrix.com/article/CTX227560 Article | Authentic ...

  8. gzip: stdin: not in gzip format 解决办法

    # sudo tar zxvf ./jdk-7ull-linux-i586.tar.gz -C /usr/lib/jvm gzip: stdin: not in gzip format tar: Ch ...

  9. includes()函数的用法

    在ES5,Array已经提供了indexOf用来查找某个元素的位置,如果不存在就返回-1,但是这个函数在判断数组是否包含某个元素时有两个小不足,第一个是它会返回-1和元素的位置来表示是否包含,在定位方 ...

  10. cookie 和session的关联关系

    session 1.1 数据存储,存服务器端, 浏览器解决http无状态问题的一种解决方案 登录,同一客户端访问服务端的时候,服务端都知道是这一个客户端 cookie 2.1 数据存储 , 存客户端 ...