Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the number in O(n) time & constant space.

**Examples :**

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

##
Recommended: Please solve it on “__PRACTICE__ ” first, before moving on to the solution.

__PRACTICE__## Python3

`# Python program to find the element occurring ` `# odd number of times ` ` ` `# function to find the element occurring odd ` `# number of times ` `def` `getOddOccurrence(arr, arr_size): ` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, arr_size): ` ` ` `count ` `=` `0` ` ` `for` `j ` `in` `range` `(` `0` `, arr_size): ` ` ` `if` `arr[i] ` `=` `=` `arr[j]: ` ` ` `count` `+` `=` `1` ` ` ` ` `if` `(count ` `%` `2` `!` `=` `0` `): ` ` ` `return` `arr[i] ` ` ` ` ` `return` `-` `1` ` ` ` ` `# driver code ` `arr ` `=` `[` `2` `, ` `3` `, ` `5` `, ` `4` `, ` `5` `, ` `2` `, ` `4` `, ` `3` `, ` `5` `, ` `2` `, ` `4` `, ` `4` `, ` `2` `] ` `n ` `=` `len` `(arr) ` `print` `(getOddOccurrence(arr, n)) ` ` ` `# This code has been contributed by ` `# Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

**Output:**

5

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.

**Program :**

## Python

` ` `# Python program to find the element occurring odd number of times ` ` ` `def` `getOddOccurrence(arr): ` ` ` ` ` `# Initialize result ` ` ` `res ` `=` `0` ` ` ` ` `# Traverse the array ` ` ` `for` `element ` `in` `arr: ` ` ` `# XOR with the result ` ` ` `res ` `=` `res ^ element ` ` ` ` ` `return` `res ` ` ` `# Test array ` `arr ` `=` `[ ` `2` `, ` `3` `, ` `5` `, ` `4` `, ` `5` `, ` `2` `, ` `4` `, ` `3` `, ` `5` `, ` `2` `, ` `4` `, ` `4` `, ` `2` `] ` ` ` `print` `"% d"` `%` `getOddOccurrence(arr) ` |

*chevron_right*

*filter_none*

**Output:**

5

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

## Recommended Posts:

- Python program to search for the minimum element occurring consecutively n times in a matrix
- Python program to find the most occurring character and its count
- Python - Remove all duplicate occurring tuple records
- Python - Maximum occurring Substring from list
- Python - Characters occurring in multiple Strings
- Python Program for Find sum of odd factors of a number
- Python program to check if given value occurs atleast k times
- Python Program for Number of elements with odd factors in given range
- Python Program to create an OTP by squaring and concatenating the odd digits of a number
- Python Program for Odd-Even Sort / Brick Sort
- Python Program for Check if count of divisors is even or odd
- Python Program for Difference between sums of odd and even digits
- Python program to copy odd lines of one file to other
- Python program to count Even and Odd numbers in a List
- Python program to print all odd numbers in a range
- Python program to print odd numbers in a List
- Python | Add similar value multiple times in list
- Python | Check if k occurs atleast n times in a list
- Python | Repeat each element K times in list
- Python | Remove elements of list that are repeated less than k times