Leetcode 448: Find All Numbers Disappeared in an Array

Problem Definition

Given an array of integers, return a list of all [1,n] inclusive elements that are not present in the input. -- Leetcode 448

Input nums: [Int]

Output missing: [Int]


Solution

    def find_missing_numbers(nums):
        return set(range(1, len(nums) + 1) - set(nums)

Reasoning and Explanation

This was a rather straightforward exercise which can be reframed as a simple set difference. Once we create sets for the expected elements and for the actual elements, we can quickly return the difference between them. This could be made more efficient in terms of space, but the expressiveness of this single line of code makes the intent behind it clear.


Tags: leetcode python


Copyright © 2021 Justin Chin
Powered by Cryogen
Theme by KingMob