Skip to content

Instantly share code, notes, and snippets.

@hzhu
Created June 22, 2016 19:33
Show Gist options
  • Save hzhu/c3421e0883ef6b26a501d9be2f2de200 to your computer and use it in GitHub Desktop.
Save hzhu/c3421e0883ef6b26a501d9be2f2de200 to your computer and use it in GitHub Desktop.
occurances of a string
// 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