#3450
Easy Algorithms Maximum students on a single bench
Array Hash Table
87.9% acceptance
Mar 31, 2026
15
1
You are given a 2D integer array of student data students, where students[i] = [student_id, bench_id] represents that student student_id is sitting on the bench bench_id.
Return the maximum number of unique students sitting on any single bench. If no students are present, return 0.
Note: A student can appear multiple times on the same bench in the input, but they should be counted only once per bench.
Solution
Rust
Time O(n)
Space O(n)
use std::collections::{HashMap, HashSet};
impl Solution {
pub fn max_students_on_bench(students: Vec<Vec<i32>>) -> i32 {
let mut bench_map: HashMap<i32, HashSet<i32>> = HashMap::new();
for s in &students {
bench_map.entry(s[1]).or_default().insert(s[0]);
}
bench_map.values().map(|s| s.len() as i32).max().unwrap_or(0)
}
}