#448
Easy Algorithms Find all numbers disappeared in an array
Array Hash Table
63.8% acceptance
Jan 13, 2026
10307
549
Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn find_disappeared_numbers(mut nums: Vec<i32>) -> Vec<i32> {
for i in 0..nums.len() {
let idx = (nums[i].abs() - 1) as usize;
if nums[idx] > 0 {
nums[idx] = -nums[idx];
}
}
let mut result = Vec::new();
for i in 0..nums.len() {
if nums[i] > 0 {
result.push((i + 1) as i32);
}
}
result
}
}