----- Original Message ----- 
From: "jmfillman" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Wednesday, January 23, 2008 10:25 PM
Subject: [flexcoders] Array Item Counting


>I have an array with several dynamically populated numbers. The array 
> is then sorted to group identical values together.
> 
> Let's say my array contains the following:[15,15,35,35,35,35, 
> 35,55,55,75,95,105,115,115,115]
> 
> What I need to do is get a count of each number, and determine the 
> highest frequency, in the above example, the number 35 is listed 4 
> times, so I would need to set a variable = 4. I've been playing around 
> with a for loop. Since I have 168 potential matches (15, 35, 55, 75, 
> 95, all the way to 2875) incrementing by 20, that's a bit excessive for 
> a bunch of if statements. It seems like a nested for loop could do 
> this, but I can't figure it out. Maybe there's an even better way I 
> haven't considered.
> 
> for (var rowCount:int=0;rowCount < arrayRows.length;rowCount++){
>     var my15:int;
>     if (arrayRows[rowCount].row == 15){
> my15 = my15+1;
> trace (my15);
>     }
> }

var myArr:Array = [15,15,35,35,35,35,35,55,55,75,95,105,115,115,115];
var longest:Number=0;
var curRun:Number=1;
var curr:Number;
var prev:Number = myArr[0]-1;

for (var idx:Number=0; idx<myArr.length; idx++){
    curr = myArr[idx];
     if (curr == prev) {
          curRun++;
     } else {
          if (curRun > longest) {
               longest = curRun;
          }
      curRun =1;
      prev=curr;
     }
}
trace(longest);

Reply via email to