Duplicate In Array

You are given an array ‘ARR’ of size ‘N’ containing each number between 1 and ‘N’ - 1 at least once. There is a single integer value that is present in the array twice. Your task is to find the duplicate integer value present in the array.

For example:

Consider ARR = [1, 2, 3, 4, 4], the duplicate integer value present in the array is 4. Hence, the answer is 4 in this case.
Note :
A duplicate number is always present in the given 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 a single integer, 'N', denoting the number of elements in the array.
The second line of each test case contains 'N' space-separated integers denoting the elements of the array 'ARR'.
Output Format:
For each test case, print a single integer - the duplicate element in the array.

Print the output of each test case in a separate line.
Constraints:
1 <=  T  <= 10
2 <=  N <= 10 ^ 5
1 <=  ARR[i]  <= N - 1

Where 'T' denotes the number of test cases, 'N' denotes the number of elements in the array, and 'ARR[i]' denotes the 'i-th' element of the array 'ARR'.

Time limit: 1 sec
CodingNinjas
author
2y

Tip 1 : Use the filter() method in Javascript
Tip 2 : Using a Set() Method in Javascript
Tip 3 : Using the reduce() Method in Javascript

CodingNinjas
author
2y
Brute Force

A simple method is to traverse through the array ARR to find the frequency of each number in the given array, and we will check if the frequency of the number is more than 1.

Therefore, our...read more

CodingNinjas
author
2y
Dynamic Programming

The idea is to observe the fact that all array elements contain a value between 1 to N - 1. Our approach will be to construct an array to store the frequency of each element in the ...read more

CodingNinjas
author
2y
Floyd’s cycle finding algorithm

The idea is to maintain two pointers, fast and slow. The pointer slow goes forward one step, and the pointer fast goes forward two steps each time. The two pointers meet...read more

CodingNinjas
author
2y
XOR-based approach

The idea is to observe the fact that the XOR of two same numbers gives 0. The given array contains values from 1 to N - 1 once and one duplicate value. Using this idea, we can find t...read more

Add answer anonymously...
Ernst & Young Senior Software Engineer 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