Find All Duplicates in an Array

Try to solve the Find All Duplicates in an Array problem.

Statement

Given an integer array, nums, of length nn, where all the integers of nums are in the range [1,n][1, n] and each integer appears once or twice, return an array of all the integers that appear twice.

Note: Make sure to write an algorithm that runs in O(n)O(n) time and uses only constant extra space.

Constraints:

  • n==n == nums.length

  • 1n1031 \leq n \leq 10^3

  • 11 \leq nums[i] n\leq n

  • Each element in nums appears once or twice.

Examples

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.