-
Notifications
You must be signed in to change notification settings - Fork 22
/
find the odd occurence
49 lines (40 loc) · 1.09 KB
/
find the odd occurence
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
/*
Given an array of positive integers where all numbers occur even number of times except one number which occurs odd number of times. Find the
number.
Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two
lines. The first line of each test case consists of an integer N, where N is the size of array. The second line of each test case contains N
space separated integers denoting array elements.
Output:
Corresponding to each test case, print the number which occur odd number of times. If no such element exists, print 0.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 107
1 ≤ A[i] ≤ 106
Example:
Input:
1
5
8 4 4 8 23
Output:
23
Explanation:
Testcase 1: 23 is the element which occurs odd number of times.
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin>>t; while(t--){
int n; cin>>n;
int a[n],flag=0;
for(int i=0; i<n; i++){
cin>>a[i];
}
flag=a[0];
for(int i=1; i<n; i++){
flag=flag^a[i];
}
cout<<flag<<endl;
}
return 0;
}