#3731
Easy Algorithms Find missing elements
Array Hash Table Sorting
82.9% acceptance
Feb 24, 2026
70
3
You are given an integer array nums consisting of unique integers.
Originally, nums contained every integer within a certain range. However, some integers might have gone missing.
The smallest and largest integers of the original range are still present in nums.
Return a sorted list of all the missing integers in this range. If no integers are missing, return an empty list.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn find_missing_elements(nums: Vec<i32>) -> Vec<i32> {
let lo = *nums.iter().min().unwrap();
let hi = *nums.iter().max().unwrap();
let set: std::collections::HashSet<i32> = nums.into_iter().collect();
(lo..=hi).filter(|x| !set.contains(x)).collect()
}
}