forked from mandliya/algorithms_and_data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_odd_one_out.cpp
37 lines (33 loc) · 873 Bytes
/
find_odd_one_out.cpp
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
/**
* Given a vector of numbers, only one number occurs odd number of times, find the number
* Example - { 1, 1, 2, 2, 2, 2, 3, 3, 3} ==> Answer 3
* Approach - XOR of number with itself is 0, so even numbers will cancel out
* and we will be left with odd number.
*/
#include <iostream>
#include <vector>
int find_odd_one_out( const std::vector<int> & vec )
{
int check = 0;
for ( auto i : vec )
{
check ^= i;
}
return check;
}
void printVector( const std::vector<int> & vec )
{
for ( auto & i : vec ) {
std::cout << i << " ";
}
std::cout << std::endl;
}
int main()
{
std::vector<int> vec{2, 3, 5, 4, 5, 2, 4, 3, 5, 2, 4, 4, 2};
std::cout << "Vector contains :" << std::endl;
printVector( vec );
std::cout << "Number which occurs odd time in the above vector :"
<< find_odd_one_out( vec ) << std::endl;
return 0;
}