Mode Algorithm using a Hash Table

In this challenge we will compare two methods used to calculate the mode value of a list of numbers. In Maths, The mode is the value that appears most often in a set of data. For instance, considering the following list of numbers:

list = [14, 8, 25, 14, 32, 8, 8, 32, 7, 8]​

The mode is 8 as the value 8 appears 4 times. You will notice that it is easier to spot the mode of a list of numbers when the numbers are sorted. e.g.:
list = [7, 8, 8, 8, 8, 14, 14, 25, 32, 32]​

Method 1: Iterative Approach, using a sorted list

mode-iterative-apporach
Python Code:

Method 2: Using a hash table to store the frequency of each value

This method relies on the use of a hash table (a.k.a dictionary data strucutre) to store the frequency of each distinct value of the list.

mode-algorithm-using-hash-table
Python Code:

Your Task

Both of the above algorithm will return the mode of a list. However, on occasion, there could be more than one mode value: if there are multiple numbers that occur with equal frequency, and more times than the others in the set. For instance:

list = [7, 8, 8, 8, 8, 14, 14, 25, 30, 30, 30, 30, 32, 32]​

The above list is bimodal as it has two modes: 8 and 30.

Your task is to adapt both of the above algorithms to make sure they identify all the mode values from a given list of numbers.

Did you like this challenge?

Click on a star to rate it!

Average rating 3.2 / 5. Vote count: 5

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: