Duplicate Elements in Array

You are provided with an array or list called ARR, consisting of N integers. These integers fall within the range from 0 to N - 1. Some elements in this array may appear more than once. Your task is to identify all such duplicate elements within the array.

Input:

 - The first line contains an integer T, representing the number of test cases.
 - For each test case, the first line includes an integer N, indicating the size of the array.
 - The second line for each test case consists of N space-separated integers representing the array elements.

Output:

 - For each test case, output a line containing K space-separated integers, which are the duplicate elements found in the array.

Example:

Input:
2
5
1 3 1 3 4
6
0 1 2 3 4 5
Output:
1 3

Explanation:

In the first test case, the numbers 1 and 3 appear twice. In the second test case, all numbers are unique, so no duplicates are output, resulting in an empty line.

Constraints:

  • 1 <= T <= 102
  • 1 <= N <= 104
  • 0 <= ARR[i] <= N - 1
  • Time Limit: 1 sec

Note:

  • All elements are within the range 0 to N - 1.
  • Elements may not appear in sorted order.
  • You may return duplicates in any order.
  • If no duplicates exist, return an empty array.
AnswerBot
4mo

Identify duplicate elements in an array of integers within the range from 0 to N - 1.

  • Iterate through the array and keep track of seen elements using a hash set.

  • If an element is already in the set, it ...read more

Help your peers!
Select
Add answer anonymously...

Directi Software Developer interview questions & answers

A Software Developer was asked Q. Stock Buy and Sell Problem Statement Given an array/list 'prices' where the elem...read more
A Software Developer was asked Q. Box Stacking Problem Statement Consider you are provided with 'n' different type...read more
A Software Developer was asked Q. Find Triplets with Given Sum Problem Statement Given an array ARR consisting of ...read more

Popular interview questions of Software Developer

A Software Developer was asked Q1. Stock Buy and Sell Problem Statement Given an array/list 'prices' where the elem...read more
A Software Developer was asked Q2. Box Stacking Problem Statement Consider you are provided with 'n' different type...read more
A Software Developer was asked Q3. Find Triplets with Given Sum Problem Statement Given an array ARR consisting of ...read more
Directi Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits