#3692
Easy Algorithms Majority frequency characters
Hash Table String Counting
67.1% acceptance
Feb 25, 2026
50
12
You are given a string s consisting of lowercase English letters.
The frequency group for a value k is the set of characters that appear exactly k times in s.
The majority frequency group is the frequency group that contains the largest number of distinct characters.
Return a string containing all characters in the majority frequency group, in any order. If two or more frequency groups tie for that largest size, pick the group whose frequency k is larger.
Solution
Rust
Time O(n log n)
Space O(n)
impl Solution {
pub fn majority_frequency_group(s: String) -> String {
use std::collections::HashMap;
let mut freq: HashMap<char, usize> = HashMap::new();
for c in s.chars() { *freq.entry(c).or_insert(0) += 1; }
// Group characters by their frequency
let mut groups: HashMap<usize, Vec<char>> = HashMap::new();
for (&c, &f) in &freq {
groups.entry(f).or_default().push(c);
}
// Find the group with max size; tiebreak by higher frequency k
let (_best_k, best_chars) = groups.iter()
.max_by(|(k1, v1), (k2, v2)| {
v1.len().cmp(&v2.len()).then(k1.cmp(k2))
})
.unwrap();
let mut result: Vec<char> = best_chars.clone();
result.sort();
result.into_iter().collect()
}
}