-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfindUniqueElement.cpp
More file actions
90 lines (64 loc) · 1.83 KB
/
findUniqueElement.cpp
File metadata and controls
90 lines (64 loc) · 1.83 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Find the Unique Element
// Given an integer array of size 2N + 1. In this given array, N numbers are present twice and one number is present only once in the array.
// You need to find and return that number which is unique in the array.
// Note : Given array will always contain odd number of elements.
// Input format :
// Line 1 : Array size i.e. 2N+1
// Line 2 : Array elements (separated by space)
// Output Format :
// Unique element present in the array
// Constraints :
// 1 <= N <= 10^6
// Sample Input :
// 7
// 2 3 1 6 3 6 2
// Sample Output :
// 1
#include<bits/stdc++.h>
using namespace std;
// arr - input array
// size - size of array
// int FindUnique(int arr[], int size){
// /* Don't write main().
// * Don't read input, it is passed as function argument.
// * Return output and don't print it.
// * Taking input and printing output is handled automatically.
// */
// unordered_map<int, int> frequency;
// for(int i = 0; i < size; i++)
// {
// frequency[arr[i]]++;
// }
// for(int i = 0; i < size; i++)
// {
// if(frequency[arr[i]] == 1)
// {
// return arr[i];
// }
// }
// return -1;
// }
//the following is the best approach for finding the unique element
int FindUnique(int arr[], int size){
/* Don't write main().
* Don't read input, it is passed as function argument.
* Return output and don't print it.
* Taking input and printing output is handled automatically.
*/
int uniqueNo = 0;
for(int i = 0; i < size; i++)
{
uniqueNo = uniqueNo^arr[i];
}
return uniqueNo;
}
int main() {
int size;
cin>>size;
int *input=new int[1+size];
for(int i=0;i<size;i++)
cin>>input[i];
cout<<FindUnique(input,size)<<endl;
delete [] input;
return 0;
}