Is *THIS* what you are after:
Code:
<script type="text/javascript">
function findMode( arr )
{
var counts = [ ];
for ( var a = 0; a < arr.length; ++a )
{
var elem = "E" + arr[a];
if ( counts[ elem ] == null )
{
counts[ elem ] = 1;
} else {
++counts[ elem ];
}
}
var half = 0.5 * arr.length;
for ( var elem in counts )
{
// I don't think >= half works...what if the array is simply [1,2] ?? SO I used >
if ( counts[elem] > half ) { return elem.substring(1); }
}
return "NONE";
}
var atest = [ 1, 37, 3, 2, 44, 1, 99, 1 ];
document.write( "Mode of [" + atest + "] is " + findMode(atest) + "<hr>" );
atest = [ 77, 101, 191, 91, 91, 191, 191, 191, 191 ];
document.write( "Mode of [" + atest + "] is " + findMode(atest) + "<hr>" );
</script>