Find All Duplicates in an Array

Given an integer array nums of length n where all integers are in the range [1, n] and each integer appears at most twice, return an array of all the integers that appear twice. The algorithm must run in O(n) time and use only constant auxiliary space, excluding the space needed to store the output.

Constraints

  • n == nums.length
  • 1 <= n <= 10^5
  • 1 <= nums[i] <= n
  • Each element in nums appears once or twice.
Saving… · 2 lines
class Solution:
def findDuplicates(self, nums: List[int]) -> List[int]:
Console

Click Run to execute sample tests.