Find All Numbers Disappeared in an Array
Sorting
Asked at 7 companies
3 recent
- Amazon
- Microsoft
- Tinkoff
- Bloomberg
- tcs
- Meta
Community-reported · source · updated 2026-10-08. ● asked in the last 6 months.
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 110 pointsSolved (Easy) 0/20Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Step 2
Try it on paper
5–10 minutes, no code yet
In the wild · Every “Sort by” button: Sorting a spreadsheet by price, then by name, needs a stable sort, which is why Python and JavaScript both use merge-sort-based TimSort.
About this problem
Write merge sort and partitioning yourself, then use sorted order (and in-place tricks) to make problems easy.
Read the full statement and examples on LeetCode, try it for 20–30 minutes, then come back for the walkthrough and the step-by-step visual.
Examples
Full statement on LeetCode ↗Example 1
- Input
- nums = [4, 3, 2, 7, 8, 2, 3, 1]
- Output
- [5, 6]
Walk through it step by step(13 steps · shows the approach, so try first)
- Every number is between 1 and 8, so each one has a home: number v belongs at index v − 1. Pass 1 marks a home as visited by making its value negative; pass 2 reads off the homes nobody visited. No extra memory needed.
- nums[0] is 4, whose home is index 4 − 1 = 3. Negate nums[3] (7 becomes -7): 4 is here.
- nums[1] is 3, whose home is index 3 − 1 = 2. Negate nums[2] (2 becomes -2): 3 is here.
- nums[2] is -2 (use |-2| = 2), whose home is index 2 − 1 = 1. Negate nums[1] (3 becomes -3): 2 is here.
- nums[3] is -7 (use |-7| = 7), whose home is index 7 − 1 = 6. Negate nums[6] (3 becomes -3): 7 is here.
- nums[4] is 8, whose home is index 8 − 1 = 7. Negate nums[7] (1 becomes -1): 8 is here.
- nums[5] is 2, whose home is index 1. nums[1] is already negative (we've seen 2 before), so leave it.
- nums[6] is -3 (use |-3| = 3), whose home is index 2. nums[2] is already negative (we've seen 3 before), so leave it.
- nums[7] is -1 (use |-1| = 1), whose home is index 1 − 1 = 0. Negate nums[0] (4 becomes -4): 1 is here.
- Pass 2: any value still positive sits at a home nobody visited, so the number that belongs there (index + 1) never appeared.
- nums[4] = 8 is positive, so 4 + 1 = 5 is missing.
- nums[5] = 2 is positive, so 5 + 1 = 6 is missing.
- Every other index is negative, so the missing numbers are [5, 6].
Example 2
All the same- Input
- nums = [1, 1]
- Output
- [2]
Example 3
No valid answer- Input
- nums = [1, 2, 3]
- Output
- []
- The answer can be in any order.
Code it here
Tab indents · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: optimal solution
Solution code
Optimal solutionEvery value v belongs at index v − 1. Mark that home as seen by making its value negative; afterwards, any index that's still positive is a number that never appeared.
class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
for x in nums:
home = abs(x) - 1
nums[home] = -abs(nums[home]) # seen
return [i + 1 for i, x in enumerate(nums) if x > 0]Reference solution, tested in Python, Java and C++.
Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.