Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
Last Updated :
26 Feb, 2023
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 Number Occurring Odd Number of Times link. we will solve this problem in python quickly using Reduce(expression, iterable) method.
Python3
from functools import reduce
def oddTimes( input ):
print ( reduce ( lambda a, b: a ^ b, input ))
if __name__ = = "__main__":
input = [ 1 , 2 , 3 , 2 , 3 , 1 , 3 ]
oddTimes( input )
|
Output:
3
Time complexity: O(n)
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...