View Single Post
Old 12-01-2012, 11:05 PM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,556
Thanks: 62
Thanked 4,056 Times in 4,025 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
W.T.H. This was fun.

My version that fixes both flaws. Not as compact, but easier to understand, I hope:
Code:
<script type="text/javascript">
function zonk(a,b)
{
    var m = a.length / b.length;
    if (lgt <= m ) 
    {
        if ( lgt < m ) { nmb = [ ]; }
        lgt = m;
        nmb.push( b.replace(/,/g,"") );
    } 
}

//array to test
var arr=[1,41,222,33,40,18,22,41,2,2,42,27,58];

document.write('<hr/>In the array<br/>[' + arr + ']<br/>');

var lgt = 0;
var nmb = [ ];
var str = arr.sort();
str = "," + str.join(',,') + ","
str.replace( /(,\d+,)(\1+)/g, zonk );

document.write('the number(s) ' + nmb.join(" and ") + ' appear(s) '+lgt+' times<hr/>');	
</script>
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote