Find the Most Common Response - Problem

You're analyzing survey data collected over multiple days to identify the most popular response across all participants. Given a 2D string array responses where each responses[i] represents survey responses collected on the i-th day, your task is to find the most frequently occurring response.

Important: Remove duplicate responses within each day before counting (a person can only vote once per day), but the same response can appear on different days.

If there's a tie between multiple responses with the same frequency, return the lexicographically smallest response (alphabetically first).

Example: If Day 1 has ["yes", "no", "yes"] and Day 2 has ["no", "maybe"], after removing duplicates we get ["yes", "no"] and ["no", "maybe"]. The response "no" appears 2 times total, making it the most common.

Input & Output

example_1.py โ€” Basic Case
$ Input: responses = [["yes", "no", "yes"], ["no", "maybe"], ["yes", "no"]]
โ€บ Output: "no"
๐Ÿ’ก Note: After removing duplicates within each day: Day 1: {"yes", "no"}, Day 2: {"no", "maybe"}, Day 3: {"yes", "no"}. Counting frequencies: "yes" appears 2 times, "no" appears 3 times, "maybe" appears 1 time. "no" has the highest frequency.
example_2.py โ€” Lexicographic Tie
$ Input: responses = [["apple", "banana"], ["banana", "apple"], ["cherry"]]
โ€บ Output: "apple"
๐Ÿ’ก Note: After removing duplicates: Day 1: {"apple", "banana"}, Day 2: {"banana", "apple"}, Day 3: {"cherry"}. Frequencies: "apple": 2, "banana": 2, "cherry": 1. "apple" and "banana" tie with 2 occurrences each, but "apple" is lexicographically smaller.
example_3.py โ€” Single Day
$ Input: responses = [["vote", "vote", "abstain", "vote"]]
โ€บ Output: "abstain"
๐Ÿ’ก Note: Only one day with responses. After removing duplicates: {"vote", "abstain"}. Both appear once in the deduplicated day, but "abstain" is lexicographically smaller than "vote".

Visualization

Tap to expand
๐Ÿ“Š Survey Response AnalysisStep 1: Daily Response DeduplicationDay 1 Raw["yes", "no", "yes"]3 responsesโ†’Day 1 Clean{"yes", "no"}2 uniqueDay 2{"no", "maybe"}Day 3{"yes", "no"}Step 2: Frequency Counting Across All DaysHash Table Counter"yes": 2 days"no": 3 days"maybe": 1 dayProcessing: O(n*m) timeโœ“Step 3: Find Most Common Response๐Ÿ† Winner: "no" (appears in 3 days)Beats "yes" (2 days) and "maybe" (1 day)
Understanding the Visualization
1
Daily Deduplication
Remove duplicate responses within each day (one vote per person per day)
2
Frequency Counting
Count how many days each unique response appears
3
Find Winner
Select response with highest frequency (lexicographically smallest for ties)
Key Takeaway
๐ŸŽฏ Key Insight: Use sets for efficient daily deduplication and hash tables for frequency counting - this gives us O(n*m) optimal performance while handling all edge cases including lexicographic tie-breaking.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * m)

n days and m average responses per day - each response is processed exactly once

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

k is the number of unique responses across all days for the frequency hash map

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค responses.length โ‰ค 100
  • 1 โ‰ค responses[i].length โ‰ค 100
  • 1 โ‰ค responses[i][j].length โ‰ค 10
  • responses[i][j] consists of lowercase English letters only
  • Each day has at least one response
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
28.3K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen