Skip to content

Instantly share code, notes, and snippets.

@ralphholzmann
Created June 16, 2011 03:31
Show Gist options
  • Save ralphholzmann/1028622 to your computer and use it in GitHub Desktop.
Save ralphholzmann/1028622 to your computer and use it in GitHub Desktop.
Paste this in the console at ralphholzmann.com to see the char distribution
var obj = {}, ordered = [];
$.get("js/jquery-1.6.1.min.js", function( src ) {
src.replace(/[^\w]|\d/gi, '').split('').forEach(function( c ) {
obj[ c ] ? ++obj[ c ] : ( obj[ c ] = 1 )
});
console.log( "Object containing character frequencies", $.extend({}, obj) );
(function order() {
var largest = 0,
c;
for ( var i in obj ) {
if ( obj[ i ] >= largest ) {
largest = obj[ i ];
c = i;
}
}
ordered.push( c );
delete obj[ c ];
if ( ! $.isEmptyObject( obj ) ) {
order();
}
})();
console.log( "Characters ordered by frequency", ordered );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment