Skip to main content

Find All Numbers Disappeared in an Array - Solution & Explanation

EasyArrayHash Table14 min readAsked at: Amazon, Microsoft, Meta +4
Practice this problem

Problem Statement

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.

 

Example 1:

Input: nums = [4,3,2,7,8,2,3,1]
Output: [5,6]

Example 2:

Input: nums = [1,1]
Output: [2]

 

Constraints:

  • n == nums.length
  • 1 <= n <= 105
  • 1 <= nums[i] <= n

 

Follow up: Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Approach Overview

Problem Overview: You are given an array of size n where values are in the range 1..n. Some numbers appear twice while others are missing. The task is to return all numbers from 1 to n that do not appear in the array.

Approach 1: Index Marking (O(n) time, O(1) extra space)

This approach uses the array itself as a presence marker. Since every value is between 1 and n, you can treat each value as an index reference. Iterate through the array, compute index = abs(nums[i]) - 1, and mark that position as visited by making the value negative. After this pass, any index that still contains a positive number means the corresponding value (index + 1) never appeared in the array.

The key insight is that the value range matches the index range, allowing you to encode presence directly inside the array. This avoids extra memory and keeps the runtime linear. A second iteration collects indices with positive values to build the result list. This technique is common in array problems where values map directly to positions.

Approach 2: Set-Based Method (O(n) time, O(n) space)

This method uses a hash table (or set) to track which numbers exist in the array. First iterate through the array and insert each element into a set. Then iterate from 1 to n and check whether each number exists in the set. If a number is missing from the set, add it to the result.

The implementation is straightforward and easy to reason about. Hash lookups run in average O(1) time, so the total runtime remains linear. The tradeoff is extra memory proportional to n. This version is useful when modifying the input array is not allowed or when clarity is preferred over space optimization.

Recommended for interviews: Interviewers usually expect the Index Marking technique because it demonstrates strong understanding of array constraints and in-place optimization. The set-based solution shows the basic idea quickly, but the in-place approach proves you can reduce auxiliary space while maintaining O(n) time complexity.

Approach 1: Approach 1: Index Marking

This approach uses the properties of the array and its indices to track missing numbers. By iterating through the array and using the values to mark the corresponding indices, we can identify which indices were never marked and thus determine the missing numbers.


Steps:

  1. Iterate over each element in the array nums.
  2. For each element, compute its corresponding index by subtracting 1 (as the array is zero-based).
  3. Use this index to mark the original array as visited by negating the value at that index.
  4. After processing all numbers, iterate over the array once more. The indices with positive values indicate the missing numbers, which are the result.

This C solution implements index marking by negating the values at positions corresponding to the numbers seen in the array. It iterates over the array twice: first to mark visited numbers and second to collect those which are still positive, indicating those numbers are missing.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) - where n is the number of elements in the array because we are iterating over the array twice.
Space Complexity: O(1) - as no additional data structure is used apart from the output list and input array is modified in-place.

Try this approach in the editor →

Approach 2: Approach 2: Set-Based Method

In this approach, we utilize a set to only store unique numbers from 1 to n that appear in the input array. By comparing this set to the complete range of numbers, we can directly determine those not present in the input.


Steps:

  1. Initialize a set containing all the numbers in nums.
  2. Create a result list which will store numbers not present in the set, iterating from 1 to n.
  3. For each number in the iteration, check if it is missing in the set and append to the result list if so.

In this solution, a boolean array acts as a set storing flags for numbers detected in the input. Subsequent traversal is made to seek indices flagged false, representing the missing numbers.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n)
Space Complexity: O(n), due to use of additional boolean array to track presence of numbers.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Index Marking

Time Complexity: O(n) - where n is the number of elements in the array because we are iterating over the array twice.
Space Complexity: O(1) - as no additional data structure is used apart from the output list and input array is modified in-place.

Approach 2: Set-Based Method

Time Complexity: O(n)
Space Complexity: O(n), due to use of additional boolean array to track presence of numbers.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Index MarkingO(n)O(1)Best for interviews and memory‑efficient solutions where modifying the array is allowed
Set-Based MethodO(n)O(n)When input modification is not allowed or when prioritizing clarity over space optimization

Video Solution

Find All Numbers Disappeared in an Array - Leetcode 448 - Python • NeetCode • 63,701 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Find All Numbers Disappeared in an Array easy or hard?
The problem is classified as Easy on LeetCode. The core idea is recognizing that the values map directly to array indices, enabling an in-place marking strategy that avoids additional memory.
Find All Numbers Disappeared in an Array Python/Java solution
In Python or Java, the common solution iterates through the array and marks the index corresponding to each value as negative. After that pass, another loop collects indices with positive values and returns index + 1 as the missing numbers. The algorithm runs in O(n) time.
How to solve Find All Numbers Disappeared in an Array in O(n)?
Use index marking. Iterate through the array and mark the index corresponding to each value as negative. After marking, scan the array again. Any position that remains positive indicates that the number (index + 1) never appeared in the array.
What is the best approach for Find All Numbers Disappeared in an Array?
The index marking approach is the most efficient. It uses the property that numbers are in the range 1..n and marks visited indices by negating values in the array. This runs in O(n) time and O(1) extra space, which is typically the expected interview solution.
Is Find All Numbers Disappeared in an Array asked at Google/Amazon/Meta?
This problem represents a common array manipulation pattern frequently asked in coding interviews at companies like Amazon, Google, and Meta. Variations appear in interview rounds focusing on in-place array techniques and hash-based lookups.
What data structure is used in Find All Numbers Disappeared in an Array?
Two main structures are used: arrays and hash sets. The optimal solution relies on in-place array index marking, while the simpler approach uses a hash set to track which numbers appear in the input.
What is the time complexity of Find All Numbers Disappeared in an Array?
The optimal solution runs in O(n) time because the array is scanned a constant number of times. Each element is processed once for marking and once when collecting missing numbers. Space complexity can be O(1) with index marking or O(n) when using a hash set.

Ready to solve this problem?

Practice Find All Numbers Disappeared in an Array with our built-in code editor and test cases.

Practice on FleetCode