Hackbright Code Challenges

Mode

Mode

Whiteboard

Medium

Concepts

Lists, Loops, Math

Download

mode.zip

Solution

Mode: Solution


The “mode” of a list is the set of item(s) that occur the most often. For example, in [1, 2, 2, 3], 2 is the most commonly-occurring item.

Where there is a tie, the mode is all items that are tied for most common: in [1, 1, 2, 2, 3], the mode is both 1 and 2.

In this challenge, you should write a function that returns the mode.

It should always return a set, even if there’s only one item in the set:

>>> mode([1]) == {1}
True

>>> mode([1, 2, 2, 2]) == {2}
True

If there is a tie, return all:

>>> mode([1, 1, 2, 2]) == {1, 2}
True

We’ve given you mode.py, which includes the stub of a find_mode function:

mode.py
def mode(nums):
    """Find the most frequent num(s) in nums."""