Open In App

Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function

Last Updated : 26 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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

Time complexity: O(n)
Auxiliary space: O(1)


Similar Reads

Map function and Lambda expression in Python to replace characters
Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1. Examples: Input : str = 'grrksfoegrrks' c1 = e, c2 = r Output : geeksforgeeks Input : str = 'ratul' c1 = t, c2 = h Output : rahul We have an existing solution for this problem in C++. Please refer to Replace a character c1 with c2 and c2 with c1 in a string S. We can solve th
2 min read
Intersection of two arrays in Python ( Lambda expression and filter function )
Given two arrays, find their intersection. Examples: Input: arr1[] = [1, 3, 4, 5, 7] arr2[] = [2, 3, 5, 6] Output: Intersection : [3, 5] We have existing solution for this problem please refer Intersection of two arrays link. We will solve this problem quickly in python using Lambda expression and filter() function. Implementation: C/C++ Code # Fun
1 min read
Lambda expression in Python to rearrange positive and negative numbers
Given an array of positive and negative numbers, arrange them such that all negative integers appear before all the positive integers in the array. The order of appearance should be maintained. Examples: Input : arr[] = [12, 11, -13, -5, 6, -7, 5, -3, -6] Output : arr[] = [-13, -5, -7, -3, -6, 12, 11, 6, 5]\Input : arr[] = [-12, 11, 0, -5, 6, -7, 5
2 min read
Find all array elements occurring more than ⌊N/3⌋ times
Given an array arr[] consisting of N integers, the task is to find all the array elements which occurs more than floor (n/3) times. Examples: Input: arr[] = {5, 3, 5}Output: 5Explanation:The frequency of 5 is 2, which is more than N/3(3/3 = 1). Input: arr[] = {7, 7, 7, 3, 4, 4, 4, 5}Output: 4 7Explanation:The frequency of 7 and 4 in the array is 3,
15+ min read
Python program to search for the minimum element occurring consecutively n times in a matrix
Given a matrix containing n rows. Each row has an equal number of m elements. The task is to find elements that come consecutively n times horizontally, vertically, diagonally in the matrix. If there are multiple such elements then print the smallest element. If there is no such element then print -1.Examples: Input : n = 4 mat[5][5] 2 1 3 4 5 3 2
4 min read
Python - Lambda function to find the smaller value between two elements
The lambda function is an anonymous function. It can have any number of arguments but it can only have one expression. Syntax lambda arguments : expression In this article, we will learn how to find the smaller value between two elements using the Lambda function. Example: Input : 2 5 Output : 2 Input : 7 5 Output : 5Method 1: Using lambda and min(
2 min read
Ways to sort list of dictionaries by values in Python - Using lambda function
In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge to sort dictionaries according to their values can
2 min read
Python | Find fibonacci series upto n using lambda
The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ........ In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1. Find the series of fibonacci numbers using lambda function. Code #1 : B
2 min read
Python Program Maximum of Three Number using Lambda
Here we will create a lambda function to find the maximum among three numbers in Python. Example: Input: a=10, b=24, c=15Output: 24 # using LambdaFinding Maximum Among Three Numbers in PythonBelow are some of the ways by which we can find the maximum among three numbers in Python. Using lambda with max() FunctionUsing lambda with nested if-else blo
3 min read
Nested Lambda Function in Python
Prerequisites: Python lambda In Python, anonymous function means that a function is without a name. As we already know the def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions. When we use lambda function inside another lambda function then it is called Nested Lambda Function. Example 1: #
2 min read
Practice Tags :
three90RightbarBannerImg