#2248
Easy Algorithms Intersection of multiple arrays
Array Hash Table Sorting Counting
68.6% acceptance
Feb 25, 2026
806
44
Given a 2D integer array nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.
Solution
Rust
Time O(n²)
Space O(n)
impl Solution {
pub fn intersection(nums: Vec<Vec<i32>>) -> Vec<i32> {
let mut count = vec![0u16; 1001];
let total = nums.len() as u16;
for row in &nums {
for &v in row {
count[v as usize] += 1;
}
}
let mut result: Vec<i32> = (1..=1000)
.filter(|&i| count[i] == total)
.map(|i| i as i32)
.collect();
result.sort_unstable();
result
}
}