11 Replies Latest reply on Apr 24, 2008 7:19 AM by kglad

# algorithm for mode

hello, i need an algorithm (to be used in the Actions for a button) that will find the mode in a list of numbers (14 for now). i'm using flash pro 8.
• ###### 1. Re: algorithm for mode
what type of object is the 'list of numbers"? a string? an array? properties of an object? something else?
• ###### 2. Re: algorithm for mode
If you mean to find the most frequently occuring number in a group of
numbers then you can use this:

function calcMode(a:Array)
{
var cnt = [];

for (var i = 0; i < a.length; i++) {
var curVal = a ;
var curCnt = 0;

for (var j = 0; j < a.length; j++) {
if (a[j] == curVal) {
curCnt++;
}
}
cnt
= curCnt;
}
var max = 0;
for(var i = 0; i < cnt.length; i++){
if(cnt > max){
max = i;
}
}
return a[max];
}

It may not be the most effiecient algorithm for mode, but it works...

--
Dave -
http://www.blurredistinction.com

• ###### 3. Re: algorithm for mode
Dave,

The i in brackets turned into italics after var curVal = a[ i ];
• ###### 4. Re: algorithm for mode
>>The i in brackets turned into italics after var curVal = a[ i ];

The forum software does suck, doesn't it... I use a news reader so I never
see those problems.

--
Dave -
http://www.blurredistinction.com

• ###### 5. Re: algorithm for mode
Yes it does but more importantly your script is incomplete. It is easy to see where the first [ i ] was placed but the remaining are a mystery. If you can put a space between the [ and the i as [ i ], then it will read correctly. Or use a different iterator than i --- but I know I'm incapable. Ten years of Fortran programming just doesn't go away over night.
• ###### 6. Re: algorithm for mode
Sorry, I didn't realize you couldn't even see the remainder - I thought it
was just italic.

Here's the function again with better variable names:

function calcMode(numbs:Array)
{
var totals = [];

for (var cnt1 = 0; cnt1 < numbs.length; cnt1++) {
var curVal = numbs[cnt1];
var curCnt = 0;

for (var cnt2 = 0; cnt2 < numbs.length; cnt2++) {
if (numbs[cnt2] == curVal) {
curCnt++;
}
}
totals[cnt1] = curCnt;
}

var max = 0;
for(var cnt = 0; cnt < totals.length; cnt++){
if(totals[cnt] > max){
max = cnt;
}
}
return numbs[max];
}

--
Dave -
http://www.blurredistinction.com

• ###### 7. Re: algorithm for mode
the mode is not unique so you should be returning an array of all the modes:

• ###### 8. Re: algorithm for mode
Mode is unique AFAIK - it's the most common number in a given set of
numbers... 3,3,4,5,7,3 - 3 is the mode. At least that is my understanding
of it.

--
Dave -
http://www.blurredistinction.com

• ###### 9. Re: algorithm for mode
3,3,4,5,7,3,4,4,5,5?

has 3 modes.

the mode is not unique. (and my code doesn't make sense unless the array being passed is contains numeric elements which isn't useful because you almost never want the mode of numeric data.)

so, count should be an object and the initialization of count should use count[a[ i ]] and not count[ i ].
• ###### 10. Re: algorithm for mode
Don't ever argue math, politics or anything to do with a haploid population with kglad.
• ###### 11. Re: algorithm for mode
lol.