I'm working through a few chapters on algorithms from the Discrete Math text by Rosen.
In the solutions manual there is an algorithm (specified in pseudocode) for finding the mode within a list of non-decreasing integers.
Here is the algorithm:
procedure find a mode(a_1, a_2, ... , a_n : non-decreasing integers) modecount = 0 i = 1 while i <= n value = a_i count = 1 while i <= n and a_i == value count = count + 1 i = i + 1 if count > modcount then modecount = count mode = value return mode
Now, I immediately see a problem with this algorithm. For instance, take the list $<1,2,2,2,4,4,4>$ we see that the following steps are followed, counting the element $1$ twice when it only appears in our list once.
Steps below begin upon first entry of the body of the while loop:
Step 1. Assign $a_1=1$ to $value$.
Step 2. Assign $1$ to $count$
Step 3. Check that $i=1 \leq 7$ and that $a_1=1$ equals $1=value$.
Step 4. Increase the value of $count$ by $1$ so that $count := 2$.
But we only have $1$ appearing once in our list and we have counted it twice.
Is my interpretation of this algorithm incorrect? To me $count$ should first be set to $0$.