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
If the number is visited it is turned negative
If the visited number is negative that means it has been already visited(duplicate) and hence added to the list.
Since the array values are from 1 to n the...read more
CodingNinjas
author
2y
Sorting
- The most trivial approach would be to sort the array/list ‘ARR’ and then return the duplicates.
- 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
- There is another mathematical approach by which we can solve this problem in linear time and constant space.
- For every element ARR[i], increment the ARR[i]%N’th element by n i.e. ARR[...read more
Add answer anonymously...
Top Freshworks Software Engineer interview questions & answers
Popular interview questions of Software Engineer
Stay ahead in your career. Get AmbitionBox app
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