You are given a 2D string array responses where each responses[i] is an array of strings representing survey responses from the ith 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.
Example 1:
Input: responses = [["good","ok","good","ok"],["ok","bad","good","ok","ok"],["good"],["bad"]]
Output: "good"
Explanation:
responses = [["good", "ok"], ["ok", "bad", "good"], ["good"], ["bad"]]."good" appears 3 times, "ok" appears 2 times, and "bad" appears 2 times."good" because it has the highest frequency.Example 2:
Input: responses = [["good","ok","good"],["ok","bad"],["bad","notsure"],["great","good"]]
Output: "bad"
Explanation:
responses = [["good", "ok"], ["ok", "bad"], ["bad", "notsure"], ["great", "good"]]."bad", "good", and "ok" each occur 2 times."bad" because it is the lexicographically smallest amongst the words with the highest frequency.
Constraints:
1 <= responses.length <= 10001 <= responses[i].length <= 10001 <= responses[i][j].length <= 10responses[i][j] consists of only lowercase English lettersProblem Overview: You receive a collection of responses (strings) and must determine which response appears most frequently. The task is essentially a frequency counting problem where you track occurrences and return the response with the highest count.
Approach 1: Brute Force Frequency Counting (O(n²) time, O(1) space)
The most direct strategy compares every response with every other response and counts how many times each appears. For each index i, iterate through the entire list and count matching values. Track the maximum frequency encountered while scanning. This approach works without additional data structures but performs redundant comparisons, making it inefficient for large inputs. Time complexity is O(n²) and space complexity is O(1). It demonstrates the basic idea of counting occurrences but rarely passes strict performance constraints.
Approach 2: Sorting + Linear Scan (O(n log n) time, O(1) or O(n) space)
Sorting groups identical responses together so frequency counting becomes a single pass. First sort the array of responses. Then iterate through the sorted list and count consecutive duplicates, updating the most frequent response whenever a larger streak appears. Sorting costs O(n log n) time, while the scan is O(n). Extra space depends on the language’s sorting implementation (often O(1) to O(n)). This method is useful when modifying the array is allowed and you want a simple implementation without additional lookup structures.
Approach 3: Hash Table Counting (O(n) time, O(n) space)
The optimal solution uses a hash table to store frequency counts while iterating once through the responses. For each string, increment its counter in the map. During the same pass (or a follow-up scan of the map), track the response with the highest frequency. Hash table lookups and updates run in constant average time, producing overall O(n) time complexity with O(n) additional space for storing counts. This approach directly leverages common patterns from array traversal and counting problems, making it both efficient and straightforward to implement.
Recommended for interviews: Interviewers expect the hash table counting approach. Starting with the brute force explanation shows you understand the problem mechanics, but transitioning to a frequency map demonstrates algorithmic optimization. The HashMap/dict solution achieves linear time and clean logic, which is typically the target complexity for problems involving frequency analysis of strings.
We can use a hash table cnt to count the occurrences of each response. For the responses of each day, we first remove duplicates, then add each response to the hash table and update its count.
Finally, we iterate through the hash table to find the response with the highest count. If there are multiple responses with the same count, we return the lexicographically smallest one.
The complexity is O(L), and the space complexity is O(L), where L is the total length of all responses.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Counting | O(n²) | O(1) | Small datasets or when avoiding extra memory is critical |
| Sorting + Linear Scan | O(n log n) | O(1)–O(n) | When sorting is acceptable and you want a simple counting pass |
| Hash Table Counting | O(n) | O(n) | General case and expected interview solution for frequency problems |
3527. Find the Most Common Response (Leetcode Medium) • Programming Live with Larry • 188 views views
Watch 1 more video solutions →Practice Find the Most Common Response with our built-in code editor and test cases.
Practice on FleetCode