Find all numbers disappeared in an array

November 5, 2022

array-and-hashmap

Problem URL: Find all numbers disappeared in an array

We will iterate over the array, and mark the element at the index of the current element as negative. Then we will iterate over the array again, and add the index of the positive element to the result.

class Solution:
    def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
        for num in nums:
            if nums[abs(num)-1] > 0:
                nums[abs(num)-1] *= -1

        res = []
        for i, num in enumerate(nums):
            if num > 0:
                res.append(i+1)

        return res

Time Complexity: O(n)
Space Complexity: O(1)