Skip to content

Commit f255eff

Browse files
committed
Number Occurring Odd Times in Array
1 parent b6ea3ce commit f255eff

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

number_occurring_odd_times.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int getOddOccurrences(int a[], int n)
5+
{
6+
int r = 0;
7+
// XORing with the previous result in every iteration
8+
for (int i=0; i<n; i++)
9+
r = r^a[i];
10+
return r;
11+
}
12+
13+
int main()
14+
{
15+
int n, o, a[50];
16+
cout<<"Enter the no. of elements in the array: ";
17+
cin>>n;
18+
cout<<"Enter the elements of the array: ";
19+
for(int i=0;i<n;i++)
20+
cin>>a[i];
21+
o = getOddOccurrences(a, n);
22+
cout<<"The number occurring odd no. of times in the given array: "<<o;
23+
return 0;
24+
}

0 commit comments

Comments
 (0)