EasyNo. 0448

Find All Numbers Disappeared in an Array — LeetCode 448 Python Solution

Pattern
Hash Map
Topics
Array, Hash Table
Reading time
2 min

The problem

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

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

Python solution

PythonSolution
class Solution:
    def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
        s = set(nums)
        return [x for x in range(1, len(nums) + 1) if x not in s]

Complexity

Time
O(n)
Space · auxiliary
O(n)

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 448. Find All Numbers Disappeared in an Array is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Related problems
No.ProblemDifficulty
0447Number of BoomerangsMedium
0442Find All Duplicates in an ArrayMedium
04544Sum IIMedium
0457Circular Array LoopMedium
0001Two SumEasy
0036Valid SudokuMedium

Frequently asked questions

How hard is LeetCode 448. Find All Numbers Disappeared in an Array?

LeetCode 448. Find All Numbers Disappeared in an Array is rated Easy on LeetCode.

What is the time complexity of LeetCode 448. Find All Numbers Disappeared in an Array?

The Python solution on this page runs in O(n).

What is the space complexity of LeetCode 448. Find All Numbers Disappeared in an Array?

The Python solution on this page uses O(n) auxiliary space.

What topics does LeetCode 448. Find All Numbers Disappeared in an Array cover?

LeetCode 448. Find All Numbers Disappeared in an Array is tagged Array and Hash Table on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.