Created
June 5, 2022 20:51
-
-
Save AitorAlejandro/006385ab8b640cde3c51cedffc64ea81 to your computer and use it in GitHub Desktop.
Compute how is the frequency of each char in a string. Returns a Map.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function computeFrequency(input: string): Map<string, number> { | |
const freqTable = new Map(); | |
for (let ch of input) { | |
if (!freqTable.has(ch)) { | |
freqTable.set(ch, 1); | |
} else { | |
freqTable.set(ch, freqTable.get(ch) + 1); | |
} | |
} | |
return freqTable; | |
} | |
console.log(computeFrequency("12345")); // Map (5) {"1" => 1, "2" => 1, "3" => 1, "4" => 1, "5" => 1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment