Tuesday, April 20, 2021

Python: Find the missing element

 Problem Statement: Consider array/list of non-negative integers. The second array/list is formed by shuffling the elements of the first array/list and deleting a random element. Given these two arrays/lists, find which element is missing in second array.

e.g.

list1 = [1, 2, 3, 4, 5, 6]

list2 = [2, 5, 4, 6, 3]

here missing element is 1.

Solution:

def missing_ele(list1, list2):

    list1.sort()
    list2.sort()

    for num1, num2 in zip(list1, list2):
        if num1 != num2:
            return num1
    return list1[-1]

list1 = [1, 2, 3, 4, 5, 6]
list2 = [2, 4, 6, 3, 5]
print("Missing element is: ", missing_ele(list1, list2))

Output: Missing element is: 1

No comments:

Post a Comment

Some useful parameters

Dielectric constant  Metals have infinite Dielectric constant  As usually metals permittivity is very large than of free space hence its Die...