Hackbright Code Challenges

Mode: Solution

Mode: Solution

Problem

Mode

Whiteboard

Medium

Concepts

Lists, Loops, Math

Download

mode-solution.zip


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

    # START SOLUTION

    # Make dictionary of {num:frequency}

    num_count = {}

    for num in nums:
        num_count[num] = num_count.get(num, 0) + 1

    # Find the *count* of highest letter

    highest_count = max(num_count.values())

    # For every number with that count, add to set of mode

    mode = set()

    for num, count in num_count.items():
        if count == highest_count:
            mode.add(num)

    return mode