#3527
Medium Algorithms Find the most common response
Array Hash Table String Counting
75.0% acceptance
Feb 25, 2026
57
7
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 after removing duplicates within each day.
If there is a tie, return the lexicographically smallest response.
Solution
Rust
Time O(n²)
Space O(n)
impl Solution {
pub fn find_common_response(responses: Vec<Vec<String>>) -> String {
use std::collections::{HashMap, HashSet};
let mut freq: HashMap<String, usize> = HashMap::new();
for day in &responses {
let unique: HashSet<&String> = day.iter().collect();
for s in unique {
*freq.entry(s.clone()).or_insert(0) += 1;
}
}
freq.into_iter()
.max_by(|(ka, va), (kb, vb)| va.cmp(vb).then(kb.cmp(ka)))
.map(|(k, _)| k)
.unwrap_or_default()
}
}