[Algorithm] Max Chars Problem
Posted answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Algorithm] Max Chars Problem相关的知识,希望对你有一定的参考价值。
// --- 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的主要内容,如果未能解决你的问题,请参考以下文章
iPhone 13(Pro、Max、Mini)和旧款 iPhone 的所有媒体查询
13 pro msx VS 12 pro max VS 三星s21 ultra 白天视频照片对比