# Dominant number in array

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• 12-02-2012, 12:50 AM
Old Pedant
Quote:

Originally Posted by 007julien
There is never two dominant numbers !

Well, true, if the definition of dominant is "more than half the length of the array". But I was just following the lead of your original code.
• 12-02-2012, 12:53 AM
evaaa
Thank you, exactly this. Much better way than mine.
• 12-02-2012, 04:32 AM
donna1
why do you keep talking about more than half?
as i pointed out earlier the Mode isnt always as common as half the elements

Dominant number is not actually a known mathematical term so i assume he ment mode?
• 12-02-2012, 01:37 PM
Philip M
Quote:

Originally Posted by donna1
why do you keep talking about more than half?
as i pointed out earlier the Mode isnt always as common as half the elements

Dominant number is not actually a known mathematical term so i assume he ment mode?

Not so. :(

http://stackoverflow.com/questions/9...er-in-an-array

A real number in the array is called a decimal dominant if it occurs more than n/10 times in the array.

But I am not sure what the practical use of this.
• 12-02-2012, 03:59 PM
007julien
This kind of script can be useful to study relative frequencies of letters in the English language... Here is an example.

The issue is: The frequency variations can they give an indication of the author's speech?
• 12-03-2012, 08:24 AM
rnd me
is there anything map/filter can't do?

Code:

```r=[ 77, 101, 191, 91, 91, 191, 191, 191, 191,343 ]   .map(function(a){return this[a]?(this[a]+=1):(this[a]=1),this;},[])[0] r.indexOf(Math.max.apply(0,r.filter(Number))); // === 191```
• 12-03-2012, 08:43 AM
Philip M
Quote:

Originally Posted by rnd me
is there anything map/filter can't do?

Code:

```r=[ 77, 101, 191, 91, 91, 191, 191, 191, 191,343 ]   .map(function(a){return this[a]?(this[a]+=1):(this[a]=1),this;},[])[0] r.indexOf(Math.max.apply(0,r.filter(Number))); // === 191```

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.
• 12-03-2012, 09:43 AM
rnd me
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.
• 12-03-2012, 10:43 AM
Philip M
Quote:

Originally Posted by rnd me
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.

Yep, it does! :)
• 12-03-2012, 02:56 PM
007julien
Thanks ! is it very necessary to return many values with map ?

Code:

```var r=[ 77, 101, 141, 91, 91, 191, 191, 343, 44, 44, 65 ],lttFrq=[];// an array or an object   function count(a){lttFrq[a]?lttFrq[a]+=1:(lttFrq[a]=1);}   r.map(count);   alert(lttFrq);```
Then (for IE only9 web users) filter give the «dominant» value, or all others frequencies...
• 12-03-2012, 07:28 PM
rnd me
Quote:

Originally Posted by 007julien
Thanks ! is it very necessary to return many values with map ?

no, i returned an array to avoid a variable.
• 12-04-2012, 02:06 AM
Old Pedant
Of course, the original poster *did* make the requirement that, *FOR HIM*, "dominant" implied that the number had to appear more than half the time.

Qoute from post #3:
Quote:

Yes exactly. I mean more times than the half
So all the stuff I did, and RndMe did, to produce multiple answers in the case of duplicates is fun but not, per his statement, needed.
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12