Skip to main content

Find Missing Elements - Solution & Explanation

EasyArrayHash TableSorting6 min readAsked at: Google, Bloomberg
Practice this problem

Problem Statement

You are given an integer array nums consisting of unique integers.

Originally, nums contained every integer within a certain range. However, some integers might have gone missing from the array.

The smallest and largest integers of the original range are still present in nums.

Return a sorted list of all the missing integers in this range. If no integers are missing, return an empty list.

 

Example 1:

Input: nums = [1,4,2,5]

Output: [3]

Explanation:

The smallest integer is 1 and the largest is 5, so the full range should be [1,2,3,4,5]. Among these, only 3 is missing.

Example 2:

Input: nums = [7,8,6,9]

Output: []

Explanation:

The smallest integer is 6 and the largest is 9, so the full range is [6,7,8,9]. All integers are already present, so no integer is missing.

Example 3:

Input: nums = [5,1]

Output: [2,3,4]

Explanation:

The smallest integer is 1 and the largest is 5, so the full range should be [1,2,3,4,5]. The missing integers are 2, 3, and 4.

 

Constraints:

  • 2 <= nums.length <= 100
  • 1 <= nums[i] <= 100

Approach Overview

Problem Overview: You receive an array containing numbers from a defined range, but some values are missing. The task is to identify which elements from the expected range do not appear in the array.

Approach 1: Sorting and Linear Scan (Time: O(n log n), Space: O(1) or O(n))

Sort the array first, then iterate through it while tracking the expected sequence of numbers. Whenever the current value is greater than the expected number, all numbers in between are missing. This approach works because sorting places elements in ascending order, allowing a single pass to detect gaps. Sorting adds an O(n log n) cost, but the scanning step itself is O(n). This technique is useful when the array can be safely reordered and you prefer avoiding extra hash structures.

Approach 2: Hash Table Lookup (Time: O(n), Space: O(n))

Store all elements from the array in a hash set. Then iterate through the expected range of values and check whether each number exists in the set. Missing elements are those that fail the membership check. Hash lookups run in average O(1) time, so the entire process becomes O(n). This approach is straightforward and efficient because it separates presence tracking from detection logic. It relies on constant-time membership checks provided by a Hash Table.

Approach 3: Boolean Marker Array (Time: O(n), Space: O(n))

Create a boolean array of size equal to the expected range. Traverse the input array and mark the index corresponding to each value as present. After marking, iterate through the marker array to collect indices that remain unmarked. Those indices represent missing numbers. This method behaves similarly to a hash set but replaces hashing with direct indexing. It works best when the range of numbers is small and contiguous, a common scenario in Array problems.

Recommended for interviews: The hash table approach is usually the expected solution. It demonstrates understanding of constant-time lookups and clean separation between data storage and verification. Interviewers may accept the Sorting approach as a first step because it clearly shows how gaps reveal missing values, but the O(n) hash-based solution signals stronger algorithmic intuition.

Solution

We first find the minimum and maximum values in the array nums, denoted as mn and mx. Then we use a hash table to store all elements in the array nums.

Next, we iterate through the interval [mn + 1, mx - 1]. For each integer x, if x is not in the hash table, we add it to the answer list.

The time complexity is O(n) and the space complexity is O(n), where n is the length of the array nums.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Sorting + ScanO(n log n)O(1) or O(n)When modifying or sorting the array is acceptable and simplicity matters
Hash TableO(n)O(n)General case with unsorted arrays where fast lookups are needed
Boolean Marker ArrayO(n)O(n)When the value range is known and small enough for direct indexing

Video Solution

3731. Find Missing Elements (Leetcode Easy) • Programming Live with Larry • 401 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Find Missing Elements easy or hard?
Find Missing Elements is generally classified as an easy problem. The logic involves basic array traversal and using a set for quick lookups. The main skill being tested is recognizing that hashing reduces the search time from O(n^2) to O(n).
Find Missing Elements Python/Java solution
In Python, developers typically use a set for constant-time membership checks. Java implementations often rely on HashSet for the same behavior. Both approaches iterate through the expected range and add numbers that are not found in the set to the result list.
How to solve Find Missing Elements in O(n)?
Store all elements from the array in a hash set, then iterate through the expected range of numbers. For each value, perform a constant-time membership check in the set. If the number is not present, add it to the result list. This produces an O(n) time solution with O(n) space.
What is the best approach for Find Missing Elements?
The hash table approach is typically the best solution. Insert all array elements into a set and iterate through the expected numeric range to check which values are absent. Each lookup runs in O(1) average time, so the full algorithm runs in O(n) time with O(n) extra space.
Is Find Missing Elements asked at Google/Amazon/Meta?
Missing-number style problems frequently appear in interviews at companies like Amazon, Google, and Meta. Variants include finding a single missing number, multiple missing numbers, or detecting gaps in a sequence. These problems test array traversal, hashing, and reasoning about time complexity.
What data structure is used in Find Missing Elements?
The most common data structure is a hash table or hash set. It stores elements from the array and allows O(1) average-time membership checks. Some implementations also use a boolean array when the value range is known and limited.
What is the time complexity of Find Missing Elements?
The optimal solution runs in O(n) time using a hash table or boolean marker array. Sorting-based solutions require O(n log n) time due to the sorting step. Space complexity is usually O(n) when storing seen elements.

Ready to solve this problem?

Practice Find Missing Elements with our built-in code editor and test cases.

Practice on FleetCode