Open In App

Python Set difference to find lost element from a duplicated array

Last Updated : 28 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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). 

Implementation:

Python3




# 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)


Output

[1]

Similar Reads

Java Program for Find lost element from a duplicated array
Given two arrays that 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: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9} Output: 1 1 is missing from second array. Input: arr1[] = {2, 3, 4, 5} arr2[] = {2, 3, 4, 5, 6} Output: 6 6 is missing from
5 min read
Javascript Program for Find lost element from a duplicated array
Given two arrays that 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: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9} Output: 1 1 is missing from second array. Input: arr1[] = {2, 3, 4, 5} arr2[] = {2, 3, 4, 5, 6} Output: 6 6 is missing from
4 min read
C++ Program to Find lost element from a duplicated array
Given two arrays that 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: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9} Output: 1 1 is missing from second array. Input: arr1[] = {2, 3, 4, 5} arr2[] = {2, 3, 4, 5, 6} Output: 6 6 is missing from
5 min read
Python3 Program to Find lost element from a duplicated array
Given two arrays that 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: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9} Output: 1 1 is missing from second array. Input: arr1[] = {2, 3, 4, 5} arr2[] = {2, 3, 4, 5, 6} Output: 6 6 is missing from
4 min read
Php Program to Find lost element from a duplicated array
Given two arrays that 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: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9} Output: 1 1 is missing from second array. Input: arr1[] = {2, 3, 4, 5} arr2[] = {2, 3, 4, 5, 6} Output: 6 6 is missing from
4 min read
Find lost element from a duplicated array
Given two arrays that 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: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9}Output: 11 is missing from second array.Input: arr1[] = {2, 3, 4, 5} arr2[] = {2, 3, 4, 5, 6}Output: 66 is missing from firs
15+ min read
Python | Pandas Index.duplicated()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.duplicated() function returns Index object with the duplicate values remove. Duplicated values are indicated as True values
2 min read
Python | Pandas TimedeltaIndex.duplicated
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas TimedeltaIndex.duplicated() function detects duplicate values in the given TimedeltaIndex object. It return a boolean np.ndarray
2 min read
Python | Pandas Series.duplicated()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.duplicated() function indicate duplicate Series values. The duplicated values ar
2 min read
Prevent duplicated columns when joining two Pandas DataFrames
Column duplication usually occurs when the two data frames have columns with the same name and when the columns are not used in the JOIN statement. In this article, let us discuss the three different methods in which we can prevent duplication of columns when joining two data frames. Syntax: pandas.merge(left, right, how='inner', on=None, left_on=N
5 min read
Article Tags :
Practice Tags :