Most students approached the problem as a maximum problem. Assume the
first item is the mode and find it’s frequency, then proceed through
the list each time seeing if the current node occurs more fequently
than the “mode so far.” Pretty much the same idea as find_max (but in this case, returning a list of all the modes).

Time to look deeper. We started running this on larger and larger data
sets. Things started really slowing down at about 20K. We then timed
things to get some numbers (thanks
StackOverflow).

What was going on. The students pretty quickly honed in on the line
that called L.count(L[i]) – Hidden Complexity.

We haven’t done big-O notation but the class easily saw that count had
to go through the entire data set and we ended up with an N^2
algorithm. For example, if we have 10 items, the main loop executes 10
times and each time, count goes through the entire list (10 items) as
well. If we go to 100 items, it becomes 100x100.

What to do????

Time to talk about what’s probably the most discussed instance of mode
finding - elections. The winner is “the mode of the ballots.”

Of course we don’t use the above algorithm. We usually tally or count the ballots. We go through the ballots once, each time adding one to the appropriate candidates “bucket.”

From here, it’s a short step to see that we can use a list. It’s
indices represent the grade values and the data in the list the counts
or tallies: