Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to Remove Duplicate Elements from NumPy Array
Next article icon

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)

Output
[1]

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp