Skip to main content

Find All Numbers Disappeared in an Array

Question

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array.

Example 1
Input: [4,3,2,7,8,2,3,1]
Output: [5,6]

Solution

all//Find All Numbers Disappeared in an Array.py


class Solution:
def findDisappearedNumbers(self, nums):
# create a list of all numbers from 1 to the length of nums
full_list = [i for i in range(1, len(nums) + 1)]

# create a set of numbers present in the nums list
nums_set = set(nums)

# return the numbers that are in the full list but not in the nums list
return list(set(full_list) - nums_set)