
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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 Articles
- 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
- Find the Number Occurring Odd Number of Times using Lambda expression and reduce function in Python
- C++ Program to calculate the number of odd days in given number of years
- C Program for Find sum of odd factors of a number?
- C++ program for Find sum of odd factors of a number
- Program to find most occurring number after k increments in python
- Golang Program to find the odd-occurring elements in a given array
- C Program for n-th odd number
- Find the Number of Subarrays with Odd Sum using C++
- Program to find number of sub-arrays with odd sum using Python
- Program to find count of numbers having odd number of divisors in given range in C++
- Python Program for Find sum of odd factors of a number
- Finding number that appears for odd times - JavaScript

Advertisements