In previous posts I wrote about the measures of central tendency and how to calculate the mean and the median in Python.
Now let’s see how to calculate the mode (the most common number in a set of numbers).
The first thing to do is to calculate the number of occurrences for each number (how many times it’s present in the set).
This could be done using a second list but a more elegant solution would be to use a dictionary, also called associative array : a collection of (key, value) pairs, such that each key is unique.
In this case the keys are the numbers found in the set and the values its occurrences.