Find duplicates in array

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.
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
CodingNinjas
author
2y
Sorting
  1. The most trivial approach would be to sort the array/list ‘ARR’ and then return the duplicates.
  2. After sorting the array in non-decreasing order, do the following for all the elements from i = 0 ...read more
CodingNinjas
author
2y
Array Manipulation
  1. There is another mathematical approach by which we can solve this problem in linear time and constant space.
  2. For every element ARR[i], increment the ARR[i]%N’th element by n i.e. ARR[...read more
Help your peers!
Add answer anonymously...
Zoho Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter