Higher – Count Occurrences Standard Algorithm

The count occurrences algorithm is very similar to the linear search v1. However, it includes an incrementing counter keep track of how many items it finds.

def countOcc(target,numbers):
    #Count the number of times target is found in numbers
    count=0
    for number in numbers:
        if number==target:
            count=count+1
    return count

You will notice that I have used a FOR EACH in this example as there is no requirement to track the position of the elements. This algorithm can also be used to count how many numbers are above or below a target value by simply changing the operator in the IF statment.

Leave a Reply

Your email address will not be published. Required fields are marked *