Counting Occurrences - Records
Purpose
The counting occurrences algorithm looks through a list of items and counts the number of times (occurrences) that a match is found. It again checks every item in the list and increments a counter when a match is found. It is important to only output the amount of matches after the entire list has been traversed.Â
The algorithm is just a basic linear search but using a variable to keep a running total.
Record StructureÂ
All record examples will use this record structure:
And we have made an array called pupils which contains 40 of these pupil records .
Counting Occurrences PseudocodeÂ
SearchName = Input from user
Total = 0
FOR counter = 0 TO Length(array).name
IF array(counter).name = SearchNameÂ
total = total +1
END IF
END FOR
Basic Linear Search using Records - Python
It is important to initialise the variable occurrences to 0 before beginning.
This could be used within a function to return the amount of occurrences ( which may be 0)
The .upper() method is only used when searching strings to avoid case specific searches (omit if this is not needed)