Timer is Running

Associated Course(s):
Interview Preparation
Amazon SDE Test Series

Given an array a[] of size N which contains elements from 0 to N-1, you need to find all the elements occurring more than once in the given array.

**Input:**

The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N which denotes the number of elements in the array. The second line of each test case contains n space separated integers denoting elements of the array a[].

**Output:**

Print the duplicate elements from the given array. The order of the output should be in sorted order. Print -1 if no duplicate exists.

**Your Task:**

Complete the function **duplicates()** which takes array a[] and n as input as parameters and returns a list of elements that occur more than once in the given array in sorted manner. If no such element is found return -1.

**Expected Time Complexity:** O(n).

**Expected Auxiliary Space:** O(n).

Note : The extra space is only for the array to be returned.

Try and perform all operation withing the provided array.

**Constraints:**

1 <= T <= 100

1 <= N <= 10^{5}

0 <= A[i] <= N-1, for each valid i

**Example:
Sample Input:**

2

4

0 3 1 2

5

2 3 1 2 3

**Sample Output:**

-1

2 3

**Explanation:**

**Testcase 1: **N=4 and all elements from 0 to (N-1 = 3) are present in the given array. Therefore output is -1.

**Testcase 2:** 2 and 3 occur more than once in the given array.

Author: saksham seth

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

tekuripurushotham | 64 |

omkarjadhav1191 | 63 |

visutkarsh2000 | 62 |

ayushaggarwal821 | 51 |

dixitabhyuday | 38 |

abnandi04 | 776 |

ponnadasaikesav | 450 |

KshamaGupta | 446 |

yogesh_aka_dante | 405 |

mummy | 313 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4752 |

Login to report an issue on this page.