Find All Numbers Disappeared in an Array
Easy
|
Array
Hash Table
|
Solved: Jan 31, 2026
View on LeetCode →
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Problem Description
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.
Approach
HashSet Approach (or the Time-Space Trade-off approach).
Solution
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> al = new ArrayList<>();
HashSet<Integer> set = new HashSet<>();
for (int num : nums) {
set.add(num);
}
for (int i = 1; i <= nums.length; i++) {
if (!set.contains(i)) {
al.add(i);
}
}
return al;
}
}