-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTwoNonRepeatingElement.cpp
More file actions
44 lines (37 loc) · 884 Bytes
/
TwoNonRepeatingElement.cpp
File metadata and controls
44 lines (37 loc) · 884 Bytes
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
// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
/* This function sets the values of
*x and *y to non-repeating elements
in an array arr[] of size n*/
void get2NonRepeatingNos(int arr[], int n, int* x, int* y)
{
/* Will hold Xor of all elements */
int Xor = arr[0];
/* Will have only single set bit of Xor */
int set_bit_no;
int i;
*x = 0;
*y = 0;
/* Get the Xor of all elements */
for (i = 1; i < n; i++)
Xor ^= arr[i];
set_bit_no = Xor & ~(Xor - 1);
for (i = 0; i < n; i++) {
if (arr[i] & set_bit_no)
*x = *x ^ arr[i];
else {
*y = *y ^ arr[i];
}
}
}
int main()
{
int arr[] = { 2, 3, 7, 9, 11, 2, 3, 11 };
int n = sizeof(arr) / sizeof(*arr);
int* x = new int[(sizeof(int))];
int* y = new int[(sizeof(int))];
get2NonRepeatingNos(arr, n, x, y);
cout << "The non-repeating elements are " << *x
<< " and " << *y;
}