# Duplicate In Array

Posted: 28 Nov, 2020
Difficulty: Easy

## PROBLEM STATEMENT

#### 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
`````` Approach 1

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 approach will be to iterate currentNumber from 1 to N - 1. In each iteration, we will traverse through the array ARR to find the frequency of currentNumber in the array. We will check if the frequency is more than 1, then there is a duplicate of the number currentNumber in the array ARR. In the end, we will return the duplicate integer value present in the array.

Algorithm:

• We will initialize duplicate as 0. The variable duplicate stores the duplicate element in the array.
• Iterate currentNumber from 1 to N - 1.
• We will set count as 0. The variable count stores the count of currentNumber in the array ARR.
• Iterate index from 0 to N - 1.
• We will check if ARR[index] is equal to currentNumber,
• We will Increment count by 1.
• We will check if count is more than 1,
• Update the value of duplicate with currentNumber.
• Return the variable duplicate.