442. Find All Duplicates in an Array

class Solution(object):
    def findDuplicates(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        res = []
        for i in range(len(nums)):
            idx = abs(nums[i]) -1
            if nums[idx] < 0:  # seen before
                res.append(abs(nums[i]))
            
            nums[idx] = - nums[idx] #miss and repeat >0
               
        return res

Last updated

Was this helpful?