Thread: Dominant number in array View Single Post
12-03-2012, 09:43 AM   PM User | #23
rnd me
Senior Coder

Join Date: Jun 2007
Location: Urbana
Posts: 3,968
Thanks: 10
Thanked 534 Times in 516 Posts
Quote:
 Originally Posted by Philip M Yes - report the mode when two values occur the same number of times. Obviously you can have more than one mode. Having two modes is called "bimodal". More than 2 modes is called "multi-modal". felgall's and Old Pedant's scripts do this.
my bad, i didn't understand that was required. i've never been good at math...

that does make a bit more work.
what a good opportunity to expand my functional example!

to compare a collection, i needed a variable (counts), and a way get the unique values, unique. i went ahead and named count() and pulled it out of the body to reduce clutter.

isn't it neat how those new pieces bolt right on to the existing solution? that's functional programming for ya.
Code:
```function unique(a){return this[a]?0:(this[a]=1)}
function count(a){ return this[a]?(this[a]+=1):(this[a]=1),this;}

r=[ 77, 101, 141, 91, 91,  191, 191,343 ]

counts=r.map( count, [])[0];

r.filter(function(a,b){
return counts[a]==this;
},
Math.max.apply(0, counts.filter(Number))
).filter( unique, {});```
this should output 91 and 191, right? it does.

and given[ 77, 101, 141, 91, 91, 191, 191,343, 44,44,65 ], it gives [91, 191, 44], so it's appears to be a scale-able solution.
__________________
my site (updated 13/9/26)
BROWSER STATS [% share] (2013/10/31) IE7:0.5, IE8:8.6, IE9:5.3, IE10:12.3, FF:17.7, CH:41.8, SF:8.1, MOBILE:20.4

Last edited by rnd me; 12-03-2012 at 09:47 AM..