Arrays and Hashing Array basics

54. Find All Numbers Disappeared in an Array

Easy

You are given an array nums of n integers, and every value in it is between 1 and n. A value can appear more than once, so some values from 1 to n may not appear at all.

Return every value from 1 to n that does not appear in nums, in increasing order. If every value appears, return an empty list.

Example 1

Input: nums = [5, 3, 1, 5, 2, 3, 8, 8]

Output: [4,6,7]

Explanation: The array has 8 elements, so the values to look for are 1 to 8. It holds 1, 2, 3, 5 and 8, some of them twice. The values 4, 6 and 7 never appear.

Example 2

Input: nums = [3, 1, 2]

Output: []

Example 3

Input: nums = [4, 4, 4, 4]

Output: [1,2,3]

Explanation: The only value in the array is 4. Every smaller value is missing, including 1 at the very start of the range.

Check your understanding

Work out the answer for this input before you write any code.

nums = [2, 2, 1, 1]

Constraints

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

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.