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: "b"
💡 Note: Day 1 unique responses: {a, b}. Day 2 unique responses: {b, c}. Total counts: a=1, b=2, c=1. Most frequent is 'b' with count 2.
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 the Most Common Response INPUT responses[0]: "happy" "sad" "happy" responses[1]: "sad" "angry" After removing duplicates: Day 0 unique: "happy" "sad" Day 1 unique: "sad" "angry" Duplicate "happy" removed ALGORITHM STEPS 1 Create Hash Map Track count of each response 2 Process Each Day Use Set to remove duplicates 3 Count Frequencies Increment unique responses 4 Find Maximum Tie: lexicographically smallest Hash Map: "happy": 1 "sad": 2 MAX "angry": 1 FINAL RESULT Most common response: "sad" Count: 2 occurrences (appears in both days) Frequency Analysis: "sad" --> Day 0 + Day 1 = 2 "happy" --> Day 0 only = 1 "angry" --> Day 1 only = 1 Output: "sad" Key Insight: Use a HashSet for each day to remove duplicates, then a HashMap to count frequencies across all days. This ensures each response is counted at most once per day. Time: O(n*m), Space: O(unique responses) TutorialsPoint - Find the Most Common Response | Hash Approach
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