- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# C/C++ Program to Find the Number Occurring Odd Number of Times?

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.

## Algorithm

### getNumOccurredOdd(arr, n)

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

## Example

#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"; }

## Output

6 is present odd number of times

- 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 Element Occurring Odd Number of Times in a List
- 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
- Finding number that appears for odd times - JavaScript
- Golang Program to find the odd-occurring elements in a given array
- Find the Number of Subarrays with Odd Sum using C++
- Program to find most occurring number after k increments in python
- C++ program to concatenate a string given number of times?
- How to find the one integer that appears an odd number of times in a JavaScript array?
- Find the Number of Subarrays with m Odd Numbers using C++

Advertisements