Searching my array for certain values and then displaying how many hits there are

58 Views Asked by At

I need some help with searching my array. I've created a bubble sorting filter and now I need some help with searching for certain values within it.

For example my array contains the numbers "1,4,5,7,8,5,5,4,3,2" and I want to see how many fives there are in the array.

How do I solve this problem?

1

There are 1 best solutions below

1
obscure On

Counting the occurences of a specific value is as simply as iterating over the whole array and incrementing a variable as soon as it finds that value.

var myArray:Array = [1, 4, 5, 7, 8, 5, 5, 4, 3, 2];
var counter:int = 0;
for (var a:int = 0; a < myArray.length; a++)
{
    if (myArray[a] == 5)
    {
        counter++;
    }
}
trace("occurences of 5: " + counter);