Given an array **arr[]** consisting of positive integers that occur even number of times, except one number, which occurs odd number of times. The task is to find this odd number of times occurring number.

**Examples :**

Input :arr = {1, 2, 3, 2, 3, 1, 3}Output :3Input :arr = {5, 7, 2, 7, 5, 2, 5}Output :5

**Naive Approach:** A Simple Solution is to run two nested loops. The outer loop picks all elements one by one and inner loop counts the number of occurrences of the element picked by outer loop.

## C++

`// C++ program to find the element ` `// occurring odd number of times ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the element ` `// occurring odd number of times ` `int` `getOddOccurrence(` `int` `arr[], ` `int` `arr_size) ` `{ ` ` ` `for` `(` `int` `i = 0; i < arr_size; i++) { ` ` ` ` ` `int` `count = 0; ` ` ` ` ` `for` `(` `int` `j = 0; j < arr_size; j++) { ` ` ` `if` `(arr[i] == arr[j]) ` ` ` `count++; ` ` ` `} ` ` ` `if` `(count % 2 != 0) ` ` ` `return` `arr[i]; ` ` ` `} ` ` ` `return` `-1; ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 3, 5, 4, 5, 2, ` ` ` `4, 3, 5, 2, 4, 4, 2 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `// Function calling ` ` ` `cout << getOddOccurrence(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time complexity:** O(N^{2})

**Auxiliary space:** O(1)

**Better Approach:** A **Better Solution** is to use Hashing. Use array elements as key and their counts as value. Create an empty hash table. One by one traverse the given array elements and store counts. Time complexity of this solution is O(n). But it requires extra space for hashing.

**Time complexity:** O(N)

**Auxiliary space:** O(N)

**Efficient Approach:** The **Best Solution** is to do bitwise XOR of all the elements. XOR of all elements gives us odd occurring element. Please note that XOR of two elements is 0 if both elements are same and XOR of a number x with 0 is x.

Below is the implementation of the above approach.

## C++

`// C++ program to find the element ` `// occurring odd number of times ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find element occurring ` `// odd number of times ` `int` `getOddOccurrence(` `int` `ar[], ` `int` `ar_size) ` `{ ` ` ` `int` `res = 0; ` ` ` `for` `(` `int` `i = 0; i < ar_size; i++) ` ` ` `res = res ^ ar[i]; ` ` ` ` ` `return` `res; ` `} ` ` ` `/* Driver function to test above function */` `int` `main() ` `{ ` ` ` `int` `ar[] = { 2, 3, 5, 4, 5, ` ` ` `2, 4, 3, 5, 2, ` ` ` `4, 4, 2 }; ` ` ` `int` `n = ` `sizeof` `(ar) / ` `sizeof` `(ar[0]); ` ` ` ` ` `// Function calling ` ` ` `cout << getOddOccurrence(ar, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## C

`// C program to find the element ` `// occurring odd number of times ` `#include <stdio.h> ` ` ` `// Function to find element occurring ` `// odd number of times ` `int` `getOddOccurrence(` `int` `ar[], ` `int` `ar_size) ` `{ ` ` ` `int` `res = 0; ` ` ` `for` `(` `int` `i = 0; i < ar_size; i++) ` ` ` `res = res ^ ar[i]; ` ` ` ` ` `return` `res; ` `} ` ` ` `/* Driver function to test above function */` `int` `main() ` `{ ` ` ` `int` `ar[] = { 2, 3, 5, 4, 5, ` ` ` `2, 4, 3, 5, 2, ` ` ` `4, 4, 2 }; ` ` ` `int` `n = ` `sizeof` `(ar) / ` `sizeof` `(ar[0]); ` ` ` ` ` `// Function calling ` ` ` `printf` `(` `"%d"` `, getOddOccurrence(ar, n)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time complexity:** O(N)

**Auxiliary space:** O(1)

Please refer complete article on Find the Number Occurring Odd Number of Times for more details!

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the Number Occurring Odd Number of Times
- Two odd occurring elements in an array where all other occur even times
- Find all array elements occurring more than ⌊N/3⌋ times
- GCD of elements occurring Fibonacci number of times in an Array
- First element occurring k times in an array
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times
- Times required by Simple interest for the Principal to become Y times itself
- Find even occurring elements in an array of limited range
- Find the sum of all highest occurring elements in an Array
- Find Kth most occurring element in an Array
- Count the number of vowels occurring in all the substrings of given string
- Check if the last element of array is even or odd after performing a operation p times
- Return maximum occurring character in an input string
- Maximum occurring character in an input string | Set-2
- Print the most occurring character in an array of strings
- NFA to accept strings that has atleast one character occurring in a multiple of 3
- Frequency of maximum occurring subsequence in given string
- K most occurring strings
- Smallest occurring element in each subsequence
- Count of Distinct Substrings occurring consecutively in a given String