Skip to content

Instantly share code, notes, and snippets.

@marcobiedermann
marcobiedermann / max-char.ts
Created August 16, 2022 07:27
Given a string, return the character that is most commonly used in the string.
function maxChar(str: string): string {
const map = new Map<string, number>();
let maxValue;
let maxCount = 0;
for (let char of str) {
const newCount = (map.get(char) || 0) + 1;
if (newCount > maxCount) {
maxValue = char;