Ở đây đưa ra một mảng số nguyên dương do người dùng nhập. Nhiệm vụ của chúng ta là tìm ra số lần xuất hiện số lẻ.
Ví dụ
Input : A=[2, 4, 7, 7, 4, 2, 2] Output : 2
Thuật toán
Step 1: Input Array element. Step 2: Write lambda expression and apply. Step 3: Reduce function over the input list until a single value is left. Step 4: Expression reduces the value of a^b into a single value. Step 5: a starts from 0 and b starts from 1.
Mã mẫu
# Python program to find the Number
# Occurring Odd Number of Times
# using Lambda expression and reduce function
from functools import reduce
def timeoccurrance(inp):
print ("RESULT ::>",reduce(lambda a, b: a ^ b, inp)))
# Driver program
if __name__ == "__main__":
A=list()
n1=int(input("Enter the size of the List ::"))
print("Enter the Element of List ::")
for i in range(int(n1)):
k=int(input(""))
A.append(k)
timeoccurrance(A) Đầu ra
Enter the size of the List :: 7 Enter the Element of List :: 1 2 3 2 3 1 3 RESULT ::> 3