-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find Duplicate element array in the array.txt
51 lines (43 loc) · 1.52 KB
/
Find Duplicate element array in the array.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
Sample Input 1:
2
5
4 2 1 3 1
7
6 3 1 5 4 3 2
Sample Output 1:
1
3
Solution
#include<vector>
using namespace std;
int findDuplicate(vector<int> &arr)
{
int ans = 0;
//XOR ing all array elements
for(int i = 0; i<arr.size(); i++ ) {
ans = ans^arr[i];
}
//XOR [1, n-1]
for(int i = 1; i<arr.size();i++ ) {
ans = ans^i;
}
return ans;
}