Posts Tagged ‘standard algorithm counting Occurrences’

Standard Algorithm 1 – Counting Occurrences

November 29, 2007

This is the first standard algorithm (in no particular order) that you have to know, and be able to write a pseudo code version of in the exam.

Put simply this algorithm counts the number of times an item appears in a list. The item could be a name or a number, the list is normally an array.

So for example in the following list:

“A” “B” “B” “C” “B” “A” “A” “B” “C” “B”

The algorithm would ask the user what they are looking for and then should return the results below:

Being looked for                             Number of occurrences

A                                                                          3

B                                                                          5

C                                                                          2

So you need to be able to write the pseudo code that will do this. My answer is below, see the powerpoints at the bottom for more detail:

1.1 ask for item being searched for

1.2 for each item in the array

1.3 if this item matches the item being searched for

1.4 add one to the count

1.5 end if

1.6 end loop

1.7 print out the number of times the item was found