-
-
Save hzhu/c3421e0883ef6b26a501d9be2f2de200 to your computer and use it in GitHub Desktop.
occurances of a string
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
// Given a string of text, output the characters ordered from | |
// highest to lowest, with each character paired with | |
// the number of times it occurs. | |
function displayCharOccuraces(text) { | |
window.charObj = {} | |
window.arr = [] | |
for (var i = 0; i < text.length; i++) { | |
if (!charObj[text[i]]) { | |
charObj[text[i]] = 1 | |
} else { | |
charObj[text[i]]++ | |
} | |
} | |
for (key in charObj) { | |
arr.push([key,charObj[key]]) | |
} | |
arr.sort(function (a, b) { | |
return a[1] < b[1] | |
}) | |
window.arr.forEach(function (element) { | |
console.log(element[0], element[1]) | |
}) | |
} | |
var x = displayCharOccuraces("banaaaaaaakkakkkaanaz!") | |
console.log(x) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment