Find the Most Common Response - Problem

You are given a 2D string array responses where each responses[i] is an array of strings representing survey responses from the i-th day.

Return the most common response across all days after removing duplicate responses within each responses[i].

If there is a tie, return the lexicographically smallest response.

Input & Output

Example 1 — Basic Deduplication and Counting
$ Input: responses = [["happy","sad","happy"],["sad","angry"]]
Output: "sad"
💡 Note: Day 1 unique responses: {happy, sad}. Day 2 unique responses: {sad, angry}. Total counts: happy=1, sad=2, angry=1. Most frequent is 'sad' with count 2.
Example 2 — Lexicographic Tie-Breaking
$ Input: responses = [["a","b"],["b","c"]]
Output: "a"
💡 Note: Each response appears exactly once after deduplication: a=1, b=1, c=1. All tied with frequency 1, so return lexicographically smallest: 'a'.
Example 3 — Multiple Days Same Response
$ Input: responses = [["good"],["good","bad"],["good"]]
Output: "good"
💡 Note: After deduplication per day: Day 1: {good}, Day 2: {good, bad}, Day 3: {good}. Counts: good=3, bad=1. Most frequent is 'good'.

Constraints

  • 1 ≤ responses.length ≤ 100
  • 1 ≤ responses[i].length ≤ 100
  • 1 ≤ responses[i][j].length ≤ 100
  • responses[i][j] consists of lowercase English letters only

Visualization

Tap to expand
Find Most Common Response: Input → Deduplication → Counting → OutputInputDay 1:[happy,sad,happy]Day 2:[sad,angry]DeduplicateDay 1:{happy,sad}Day 2:{sad,angry}Counthappy: 1sad: 2 ←angry: 1OutputsadMost frequentProcess: Remove duplicates per day → Count frequencies → Find maximum
Understanding the Visualization
1
Input
2D array of survey responses with duplicates within days
2
Deduplicate
Remove duplicates within each day using sets
3
Count
Count frequency of each response across all days
4
Output
Return most frequent response (lexicographically smallest on ties)
Key Takeaway
🎯 Key Insight: Always deduplicate within each day first, then count across all days to find the global winner
Asked in
Amazon 35 Google 28 Microsoft 22 Facebook 18
23.4K Views
Medium Frequency
~15 min Avg. Time
890 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