Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Dominant number in array

1. 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.

2. Thank you, exactly this. Much better way than mine.

3. 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?

4. 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.

5. 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?

6. 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```

7. 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.

8. 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.

9. 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!

10. 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);
Then (for IE only9 web users) filter give the «dominant» value, or all others frequencies...

11. 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. 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:
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.

Page 2 of 2 First 12

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•