Find All Duplicates in an Array

Problem Id: 442 Difficulty: Medium


Intuition

Solution


class Solution:
    def findDuplicates(self, nums: List[int]) -> List[int]:
        ans = []
        for x in nums:
            key = abs(x) - 1
            if nums[key] < 0:
                ans.append(abs(x))
            else:
                nums[key] *= -1
        return ans