↓Skip to main content
  1. LeetCode/

LeetCode 3731: Find Missing Elements

·1 min· ·
LeetCode Easy Hash-Table Sorting Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2025-11-02

  • Submissions: Accepted (first try)

Approach
#

The original range is [min, max], and both ends are still in the array. Enumerate every integer in the range; those not in the array are missing, and enumerating in order keeps them sorted.

Use a set for lookups (Hash Table Template 1).

My Solution
#

I originally used i not in nums, an O(n) list lookup each time; the small range let it pass. With a set:

class Solution:
    def findMissingElements(self, nums: List[int]) -> List[int]:
        seen = set(nums)
        return [x for x in range(min(nums), max(nums)) if x not in seen]

max is always present, so excluding it from the range is fine.

Complexity
#

Time O(n + R) with R = max - min, space O(n).

Mistakes to Avoid
#

  • in on a list is a linear search; switch to a set for larger ranges.

Interview Explanation
#

The minimum and maximum are still present, so I convert the array to a set and enumerate every integer between them; the ones not in the set are missing. Time O(n + R).

Related

LeetCode 594: Longest Harmonious Subsequence
·1 min
LeetCode Easy Array Hash-Table Sliding-Window Sorting
LeetCode 594 note: subsequences need not be contiguous, so just count; for each x add the counts of x and x + 1
LeetCode 2099: Find Subsequence of Length K With the Largest Sum
·1 min
LeetCode Easy Array Hash-Table Sorting Heap-Priority-Queue
LeetCode 2099 note: pick the k largest values along with their indices, then sort by index to restore the original order
LeetCode 2357: Make Array Zero by Subtracting Equal Amounts
·1 min
LeetCode Easy Array Hash-Table Greedy Sorting
LeetCode 2357 note: each operation zeroes the group of the smallest positive value, so the answer is the number of distinct positive values
LeetCode 1122: Relative Sort Array
·1 min
LeetCode Easy Array Hash-Table Sorting Counting-Sort
LeetCode 1122 note: count first, emit values in arr2’s order, then append the remaining values in ascending order
LeetCode 888: Fair Candy Swap
·2 mins
LeetCode Easy Array Hash-Table Binary-Search Sorting
LeetCode 888 note: equal sums after swapping x and y means y = x + (sumB - sumA) / 2; put B in a set and check each x from A
LeetCode 645: Set Mismatch
·2 mins
LeetCode Easy Array Hash-Table Bit-Manipulation Sorting
LeetCode 645 note: use sums: missing = sum of 1..n - sum of the set, duplicate = array sum - sum of the set