Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Find Duplicates In Array

Easy
0/40
Average time to solve is 15m
profile
Contributed by
77 upvotes
Asked in companies
InfosysAckoIBM

Problem statement

You are given an array/list 'ARR' consisting of N integers, which contains elements only in the range 0 to N - 1. Some of the elements may be repeated in 'ARR'. Your task is to find all such duplicate elements.

Note:
1. All the elements are in the range 0 to N - 1.
2. The elements may not be in sorted order.
3. You can return the duplicate elements in any order.
4. If there are no duplicates present then return an empty array.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer T, denoting the number of test cases.

The first line of each test case contains the integer N, denoting the size of the array.

The second line of each test case contains N space-separated integers denoting the array elements.
Output Format:
For each test case, every line of output contains K space-separated integers denoting the duplicate elements in the array.
Note
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <=  10^2
1 <= N <= 10^4
0 <= ARR[i] <= N - 1
Time Limit: 1 sec
Sample Input 1:
2
5
0 2 1 2 3 
7
3 2 1 3 2 1 5
Sample Output 1:
2
1 2 3
Explanation For Sample Input 1:
For the first test case, since 2 is the only duplicate element, so we return it.

For the second test case, since 1, 2, 3 are duplicates so we return them. Note that 2, 3, 1 and 3, 2, 1 are also acceptable.
Sample Input 2:
1
3
0 1 2
Sample Output 2:
-1
Explanation For Sample Input 2:
For the first test case, since there are no duplicates we return -1.
Full screen
Console