Find All Numbers Disappeared in an Array

 

448. Find All Numbers Disappeared in an Array

Solution

  1. Since the array contians number in range [1, n], we can use their value as index to track the information.

  2. When a number is visited, we mark the value it is point to negative.

  3. The positive number’s index disappeared in the array.

Reference

def findDisappearedNumbers(self, nums):
    """
    :type nums: List[int]
    :rtype: List[int]
    """
    for i in xrange(len(nums)):
        index = abs(nums[i]) - 1
        nums[index] = - abs(nums[index])

    return [i + 1 for i in range(len(nums)) if nums[i] > 0]