-
-
Save mjstromberg/8f4edd906abfb737b30d62cebceaba63 to your computer and use it in GitHub Desktop.
This file contains 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 frequencySort(string = '') { | |
const charFreqMap = {}; | |
for (let i = 0; i < string.length; i++) { | |
const char = string[i]; | |
const isCharInCharMap = Boolean(charFreqMap[char]); | |
charFreqMap[char] = isCharInCharMap ? charFreqMap[char] + 1 : 1; | |
} | |
const charArray = []; | |
for (let char in charFreqMap) { | |
const frequency = charFreqMap[char]; | |
const extrapolatedString = char.repeat(frequency); | |
charArray.push(extrapolatedString); | |
} | |
charArray.sort((a, b) => b.length - a.length); | |
return charArray.join(''); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment