Python Program to Find the Number Occurring Odd Number of Times
Last Updated :
24 Oct, 2023
Write a Python program for a given array of positive integers. All numbers occur an even number of times except one number which occurs an 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
Python Program to Find the Number Occurring Odd Number of Times using Nested Loop:
A Simple Solution is to run two nested loops. The outer loop picks all elements one by one and the inner loop counts the number of occurrences of the element picked by the outer loop.
Below is the implementation of the brute force approach :
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
Time Complexity: O(n^2)
Auxiliary Space: O(1)
Python Program to Find the Number Occurring Odd Number of Times using Hashing:
A Better Solution is to use Hashing. Use array elements as a key and their counts as values. Create an empty hash table. One by one traverse the given array elements and store counts.
Below is the implementation of the above approach:
Python3
# Python3 program to find the element
# occurring odd number of times
# function to find the element
# occurring odd number of times
def getOddOccurrence(arr,size):
# Defining HashMap in C++
Hash=dict()
# Putting all elements into the HashMap
for i in range(size):
Hash[arr[i]]=Hash.get(arr[i],0) + 1;
# Iterate through HashMap to check an element
# occurring odd number of times and return it
for i in Hash:
if(Hash[i]% 2 != 0):
return i
return -1
# Driver code
arr=[2, 3, 5, 4, 5, 2, 4,3, 5, 2, 4, 4, 2]
n = len(arr)
# Function calling
print(getOddOccurrence(arr, n))
# This code is contributed by mohit kumar
Time Complexity: O(n)
Auxiliary Space: O(n)
Python Program to Find the Number Occurring Odd Number of Times using Bit Manipulation:
The Best Solution is to do bitwise XOR of all the elements. XOR of all elements gives us odd occurring elements.
Here ^ is the XOR operators;
Note :
x^0 = x
x^y=y^x (Commutative property holds)
(x^y)^z = x^(y^z) (Distributive property holds)
x^x=0
Below is the implementation of the above approach.
Python3
# 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))
Time Complexity: O(n)
Auxiliary Space: O(1)
Please refer complete article on Find the Number Occurring Odd Number of Times for more details!
Similar Reads
Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function 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 : [1, 2, 3, 2, 3, 1, 3] Output : 3 We have existing solution for this problem please refer Find the Numbe
1 min read
Python Program for Number of elements with odd factors in given range Given a range [n,m], find the number of elements that have odd number of factors in the given range (n and m inclusive). Examples: Input : n = 5, m = 100 Output : 8 The numbers with odd factors are 9, 16, 25, 36, 49, 64, 81 and 100 Input : n = 8, m = 65 Output : 6 Input : n = 10, m = 23500 Output :
2 min read
Python Program to Count Even and Odd Numbers in a List In Python working with lists is a common task and one of the frequent operations is counting how many even and odd numbers are present in a given list. The collections.Counter method is the most efficient for large datasets, followed by the filter() and lambda approach for clean and compact code. Us
4 min read
Python Program to Count Even and Odd Numbers in a List In Python working with lists is a common task and one of the frequent operations is counting how many even and odd numbers are present in a given list. The collections.Counter method is the most efficient for large datasets, followed by the filter() and lambda approach for clean and compact code. Us
4 min read
Python Program for Check if count of divisors is even or odd Write a Python program for a given number ânâ, the task is to find its total number of divisors that are even or odd. Examples: Input : n = 10 Output: Even Input: n = 100Output: Odd Input: n = 125Output: Even Python Program for Check if count of divisors is even or odd using Naive Approach:A naive a
4 min read
Print odd numbers in a List - Python We are given a list and our task is to print all the odd numbers from it. This can be done using different methods like a simple loop, list comprehension, or the filter() function. For example, if the input is [1, 2, 3, 4, 5], the output will be [1, 3, 5].Using LoopThe most basic way to print odd nu
2 min read