L2 · Working engineerDictionaries & hashing~6 min · 5 tests#30

Most Frequent Element in a List

Find the most common value in a Python list with a frequency map in O(n), breaking ties by first appearance. Compare Counter.most_common and max().

The problem

Return the value that appears most often in nums. On a tie, return the one that appears first in the list. Return None for an empty list.

Examples

  1. Example 1

    Input

    most_frequent([1, 3, 2, 3, 4, 3, 2])

    Expected output

    3
  2. Example 2

    Input

    most_frequent([5, 1, 1, 5])

    Expected output

    5

+ 3 hidden tests on Submit — tie → first seen.

Edge cases to ask about

  • Empty list
  • Ties
  • Single element

Hints

0/3

    How an interviewer scores this

    0/9
    Python 3.13 · most_frequent
    ⌘/Ctrl + Enter runs the examples

    Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.

    Complexity Lab

    What does this cost as n grows?

    Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.

    Time complexity of the optimal solution
    Space complexity (extra memory)

    Pick both to reveal the answer.

    Measure it

    Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.

    From brute force to optimal

    The progression an interviewer wants to hear, one step at a time.

    ApproachTimeSpaceIdea
    list.count per elementO(n²)O(1)Recounts for every element.
    bestCount, then scan in orderO(n)O(n)Scanning the list in order makes the tie-break 'first appearance'.
    Walkthrough of the optimal approach (try it yourself first)

    Build the counts, then walk nums in order and keep the value with the strictly highest count. Walking in list order is what makes "first appearance" win ties.

    Counter(nums).most_common(1)[0][0] works too — Counter preserves insertion order, and most_common is stable for equal counts.

    Complexity: O(n) time, O(n) space. Two linear passes; the counts dict can hold n keys.

    Reveal the reference solution
    def most_frequent(nums):
        counts = {}
        best, best_count = None, 0
        for x in nums:
            counts[x] = counts.get(x, 0) + 1
        for x in nums:
            if counts[x] > best_count:
                best, best_count = x, counts[x]
        return best

    The brute force, for comparison

    def most_frequent(nums):
        best, best_count = None, 0
        for x in nums:
            c = nums.count(x)
            if c > best_count:
                best, best_count = x, c
        return best

    Follow-ups interviewers ask

    • Top k most frequent (bucket sort, O(n)).
    • Most frequent in a sliding window.

    Frequently asked interview questions

    Core interview concepts, complexities, and follow-ups scored by hiring teams.

    What is the time complexity of Most Frequent Element in a List in Python?

    The optimal solution runs in O(n) time and O(n) auxiliary space. Two linear passes; the counts dict can hold n keys.

    What is the brute-force approach, and how do you optimise it?

    list.count per element: O(n²) time, O(1) space. Recounts for every element. Count, then scan in order: O(n) time, O(n) space. Scanning the list in order makes the tie-break 'first appearance'.

    What follow-up questions do interviewers ask about Most Frequent Element in a List?

    Top k most frequent (bucket sort, O(n)). Most frequent in a sliding window.