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

Comments

Popular posts from this blog

Continuous Wave Radar

Digital design fundamentals

Frequency Modulated Continuous Wave Radar