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 Number Occurring Odd Number of Times link. we will solve this problem in python quickly using Reduce(expression, iterable) method.
# Python program to Find the Number # Occurring Odd Number of Times # using Lambda expression and reduce function from functools import reduce def oddTimes(input): # write lambda expression and apply # reduce function over input list # until single value is left # expression reduces value of a ^ b into single value # a starts from 0 and b from 1 # ((((((1 ^ 2)^3)^2)^3)^1)^3) print (reduce(lambda a, b: a ^ b, input)) # Driver program if __name__ == "__main__": input = [1, 2, 3, 2, 3, 1, 3] oddTimes(input) |
Output:
3
Recommended Posts:
- Map function and Lambda expression in Python to replace characters
- Intersection of two arrays in Python ( Lambda expression and filter function )
- Python lambda (Anonymous Functions) | filter, map, reduce
- Lambda expression in Python to rearrange positive and negative numbers
- The most occurring number in a string using Regex in python
- Python map function to find row with maximum number of 1's
- Python | Find fibonacci series upto n using lambda
- Python program to find number of local variables in a function
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Find all the numbers in a string using regular expression in Python
- Python | sympy.Lambda() method
- Overuse of lambda expressions in Python
- Lambda and filter in Python Examples
- Python | Find closest number to k in given list
- Python | Find number of lists in a tuple
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



