Skip to content

Instantly share code, notes, and snippets.

@AitorAlejandro
Created June 5, 2022 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AitorAlejandro/006385ab8b640cde3c51cedffc64ea81 to your computer and use it in GitHub Desktop.
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.
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