Given two arrays which are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element. Examples:
Input: A = [1, 4, 5, 7, 9] B = [4, 5, 7, 9]Output: [1]1 is missing from second array.Input: A = [2, 3, 4, 5 B = 2, 3, 4, 5, 6]Output: [6]6 is missing from first array.
We have existing solution for this problem please referFind lost element from a duplicated array. We can solve this problem quickly in python usingSet difference logic. Approach is very simple, simply convert both lists inSet and performA-B operation where len(A)>len(B).
Implementation:
Python3# Function to find lost element from a duplicate# arraydeflostElement(A,B):# convert lists into setA=set(A)B=set(B)# take difference of greater set with smalleriflen(A)>len(B):print(list(A-B))else:print(list(B-A))# Driver programif__name__=="__main__":A=[1,4,5,7,9]B=[4,5,7,9]lostElement(A,B)