Find All Duplicates in an Array

How to find all duplicates in an array without using extra space?

Given an array of integers, the integers are in the range of 1 ≤ a[i] ≤ n (n = size of array). In this Array, some elements appear twice and others appear once.

Find all the elements that appear twice in this array.

We have to solve this problem without using extra space and in O(n) time complexity.

For Example:

Input: [4, 3, 2, 7, 8, 2, 3, 1]

Output: [2, 3]