448. Find All Numbers Disappeared in an Array
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find all integers in the range [1, n] that are missing from a given array nums of length n. The key constraints are that the elements of nums are themselves within the range [1, n], and we are asked for a solution that uses O(n) time and O(1) extra space (the returned list does not count as extra space).
Solution
Find All Numbers Disappeared in an Array in Python
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
# First pass: Use the sign of the number at an index to mark presence.
# For each number `num` in the input, we go to the index `abs(num) - 1`
# and make the value at that index negative.
for num in nums:
index = abs(num) - 1
# Ensure the number at the target index is negative.
# Using abs() ensures we don't accidentally make a negative number positive.
nums[index] = -abs(nums[index])
# Second pass: Find all indices where the value is still positive.
# These indices (plus 1) correspond to the numbers that were never seen.
# A list comprehension provides a concise way to build the result.
return [i + 1 for i, num in enumerate(nums) if num > 0]
Find All Numbers Disappeared in an Array in Java
class Solution {
public List<Integer> findDisappearedNumbers(int[] nums) {
// Pass 1: In-place marking.
// For each number `num`, we find its corresponding index `index = Math.abs(num) - 1`.
// We then negate the value at `nums[index]` to mark the presence of `Math.abs(num)`.
// We must use `Math.abs` because the element at the current position might have been negated by an earlier step.
for (int i = 0; i < nums.length; i++) {
int index = Math.abs(nums[i]) - 1;
// Use Math.abs() on the value at the index to avoid making it positive again if it's already negative.
nums[index] = -Math.abs(nums[index]);
}
List<Integer> result = new ArrayList<>();
// Pass 2: Collect the numbers that are missing.
// Any index `i` where `nums[i]` is still positive corresponds to a missing number `i + 1`.
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
result.add(i + 1);
}
}
return result;
}
}
Find All Numbers Disappeared in an Array in C++
class Solution {
public:
vector<int> findDisappearedNumbers(vector<int>& nums) {
// Pass 1: In-place marking.
// For each number `num`, we find its corresponding index `index = abs(num) - 1`.
// We then negate the value at `nums[index]` to mark the presence of `abs(num)`.
// We use abs() because the values at some indices might be changed to negative in this process.
for (int num : nums) {
int index = abs(num) - 1;
// Use abs() on the value at the index to avoid making it positive again if it's already negative.
nums[index] = -abs(nums[index]);
}
vector<int> result;
// Pass 2: Collect results.
// The indices `i` where `nums[i]` is still positive correspond to missing numbers `i + 1`.
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] > 0) {
result.push_back(i + 1);
}
}
return result;
}
};
Find All Numbers Disappeared in an Array in TypeScript
function findDisappearedNumbers(nums: number[]): number[] {
// Pass 1: In-place marking.
// For each number `num` in the array, we find its corresponding index `index = Math.abs(num) - 1`.
// We then negate the value at `nums[index]` to mark that `Math.abs(num)` has been seen.
for (const num of nums) {
const index = Math.abs(num) - 1;
// Use Math.abs() on nums[index] to ensure we only negate and don't flip a negative back to positive.
nums[index] = -Math.abs(nums[index]);
}
const result: number[] = [];
// Pass 2: Collect all indices where the value is still positive.
// These indices (plus 1) correspond to the numbers that were never seen.
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
result.push(i + 1);
}
}
return result;
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.