# Python Set difference to find lost element from a duplicated array

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 refer Find lost element from a duplicated array. We can solve this problem quickly in python using Set difference logic. Approach is very simple, simply convert both lists in **Set** and perform **A-B** operation where len(A)>len(B).

`# Function to find lost element from a duplicate ` `# array ` ` ` `def` `lostElement(A,B): ` ` ` ` ` `# convert lists into set ` ` ` `A ` `=` `set` `(A) ` ` ` `B ` `=` `set` `(B) ` ` ` ` ` `# take difference of greater set with smaller ` ` ` `if` `len` `(A) > ` `len` `(B): ` ` ` `print` `(` `list` `(A` `-` `B)) ` ` ` `else` `: ` ` ` `print` `(` `list` `(B` `-` `A)) ` ` ` `# Driver program ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `A ` `=` `[` `1` `, ` `4` `, ` `5` `, ` `7` `, ` `9` `] ` ` ` `B ` `=` `[` `4` `, ` `5` `, ` `7` `, ` `9` `] ` ` ` `lostElement(A,B) ` |

*chevron_right*

*filter_none*

Output:

[1]

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find lost element from a duplicated array
- Find Array formed by adding each element of given array with largest element in new array to its left
- Python heapq to find K'th smallest element in a 2D array
- Array formed from difference of each element from the largest element in the given array
- Python | Pandas TimedeltaIndex.duplicated
- Python | Pandas Series.duplicated()
- Python | Pandas Index.duplicated()
- Python | Pandas Dataframe.duplicated()
- Replace each element by the difference of the total size of the array and frequency of that element
- Find just strictly greater element from first array for each element in second array
- Find last element after deleting every second element in array of n integers
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Generate Array whose difference of each element with its left yields the given Array
- Find an element in array such that sum of left array is equal to sum of right array
- Find element in array that divides all array elements
- Maximum difference between first and last indexes of an element in array
- Sum of minimum absolute difference of each array element
- Search an element in an array where difference between adjacent elements is 1
- Minimize sum of adjacent difference with removal of one element from array
- Find if array has an element whose value is half of array sum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.