Python Set difference to find lost element from a duplicated array Last Updated : 28 Jul, 2022 Summarize Comments Improve Suggest changes Share Like Article Like 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] Comment More infoAdvertise with us Next Article Program to print duplicates from a list of integers in Python S Shashank Mishra Follow Improve Article Tags : Python python-set Practice Tags : pythonpython-set Similar Reads How to Remove Duplicate Elements from NumPy Array In this article, we will see how to remove duplicate elements from NumPy Array. Here we will learn how to Remove Duplicate Elements from a 1-D NumPy Array and 2-D NumPy Array.Input1: [1 2 3 4 5 1 2 3 1 2 9] Output1: [1 2 3 4 5 9] Explanation: In this example, we have removed duplicate elements from 7 min read Count distinct elements in an array in Python Given an unsorted array, count all distinct elements in it. Examples: Input : arr[] = {10, 20, 20, 10, 30, 10} Output : 3 Input : arr[] = {10, 20, 20, 10, 20} Output : 2 We have existing solution for this article. We can solve this problem in Python3 using Counter method. Approach#1: Using Set() Thi 2 min read Python - Check if list contains all unique elements To check if a list contains all unique elements in Python, we can compare the length of the list with the length of a set created from the list. A set automatically removes duplicates, so if the lengths match, the list contains all unique elements. Python provides several ways to check if all elemen 2 min read Program to print duplicates from a list of integers in Python In this article, we will explore various methods to print duplicates from a list of integers in Python. The simplest way to do is by using a set.Using SetSet in Python only stores unique elements. So, we loop through the list and check if the current element already exist (duplicate) in the set, if 2 min read Python | Pandas Index.difference() 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.difference() function return a new Index with elements from the index that 2 min read NumPy| How to get the unique elements of an Array To find unique elements of an array we use the numpy.unique() method of the NumPy library in Python. It returns unique elements in a new sorted array. Example: Python3 import numpy as np arr = np.array([1, 2, 3, 1, 4, 5, 2, 5]) unique_elements = np.unique(arr) print(unique_elements) Output: [1 2 3 4 2 min read Like