- Related Questions & Answers
- Java Program to Find the Number Occurring Odd Number of Times
- C/C++ Program for Finding the Number Occurring Odd Number of Times?
- Python program to find the number occurring odd number of times using Lambda expression and reduce function
- Find the Number Occurring Odd Number of Times using Lambda expression and reduce function in Python
- C Program for Find sum of odd factors of a number?
- C++ program for Find sum of odd factors of a number
- C++ Program to calculate the number of odd days in given number of years
- C++ program to concatenate a string given number of times?
- Find sum of odd factors of a number using C++.
- C Program for n-th odd number
- Finding number that appears for odd times - JavaScript
- C program to find Decagonal Number?
- Program to find the Hidden Number in C++
- C++ program to find the Parity of a number efficiently
- How to find the one integer that appears an odd number of times in a JavaScript array?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this program we will see how we can get a number that is occurring odd number of times in an array. There are many different approaches. One of the easiest approach is performing ZOR operation. If a number is XORed with itself, it will be 0. So if a number XORed even number of times, it will be 0, otherwise the number itself.

This solution has one problem, if more than one element has odd number of occurrences, it will return one of them.

begin res := 0 for each element e from arr, do res := res XOR e done return res end

#include <iostream> using namespace std; int getNumOccurredOdd(int arr[], int n) { int res = 0; for (int i = 0; i < n; i++) res = res ^ arr[i]; return res; } int main() { int arr[] = {3, 4, 6, 5, 6, 3, 5, 4, 6, 3, 5, 5, 3}; int n = sizeof(arr)/sizeof(arr[0]); cout << getNumOccurredOdd(arr, n) << " is present odd number of times"; }

6 is present odd number of times

Advertisements