Last active
June 5, 2017 16:25
-
-
Save alexitaylor/4ef2abb86eef42a00bd5d93d1fa06fc3 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
/** | |
* @param {string} s | |
* @return {string} | |
*/ | |
var frequencySort = function(s) { | |
var characters = {}; | |
for (var i = 0; i < s.length; i++) { | |
if (s[i] in characters) { | |
characters[s[i]]++; | |
} else { | |
characters[s[i]] = 1; | |
} | |
} | |
var sortable = []; | |
for (var k in characters) { | |
sortable.push([k, characters[k]]); | |
} | |
var sorted = sortable.sort(function (a, b) { | |
return b[1] - a[1]; | |
}) | |
var string = ''; | |
for (var j = 0; j < sorted.length; j++) { | |
var character = sorted[j][0] | |
for (var l = 0; l < sorted[j][1]; l++) { | |
string += character; | |
} | |
} | |
return string; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment