Duplicates in an Array in O(n)

Duplicates in an Array in O(n)

Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear more than one time and others appear once. Find duplicate items.

Facts:

  • Total size of array is n
  • Element in array is between 1 and n
  • Some element could be repeated twice

Latest Source Code:
Github: DuplicatesNumbersInArray.java


Output:

Array : [1, 2, 2, 4]
Duplicate: [2]
Array : [4, 3, 2, 7, 8, 2, 3, 1]
Duplicate: [2, 3]
Array : [10, 2, 5, 10, 9, 1, 1, 4, 3, 7]
Duplicate: [10, 1]

Author: Hrishikesh Mishra