Python Program to find the distinct elements from two arrays


In programming, an array is a data structure that is used to store a collection of homogeneous data elements. And each element in the array is identified by a key or index value.

Arrays in python

Python doesn’t have a specific data type to represent arrays. Instead, we can use the List as an array.

[1, 4, 6, 5, 3]

Finding distinct elements from two arrays means, identifying the unique elements between the two given arrays.

Input Output Scenarios

Assume we have two arrays A and B with integer values. And the resultant array will have distinct elements from the two arrays.

Input arrays:
A = [1, 2, 3, 4, 5]
B = [5, 2, 6, 3, 9]
Output array:
[1, 6, 4, 9]

The elements 1, 6, 4, 9 are the unique values between the two arrays.

Input arrays:
A = [1, 2, 3, 4, 5]
b = [3, 4, 5, 1, 2]
Output array:
[]

There are no distinct elements found in the given 2 arrays.

Using for loop

We will use the for loop on arrays whose number of elements are equal.

Example

In the below example we will define the for loop using the list comprehension method.

arr1 = [1, 2, 3, 4, 5]
arr2 = [5, 2, 6, 3, 9]

result = []
for i in range(len(arr1)):
   if arr1[i] not in arr2:
      result.append(arr1[i])
   if  arr2[i] not in arr1:
      result.append(arr2[i])
        
print("The distinct elements are:", result)

Output

The distinct elements are: [1, 6, 4, 9]

Here we found the distinct elements by using the for loop and if conditions. Initially, Iterated the loop and verified if the element arr1[i] is not present in the array arr2 or not then we will append that element to the result variable if the element is a distinct element. In the same way, we verified the second array element to the first array. And stored the distinct elements in the result array.

Example

Let’s take another set of arrays and find the distinct elements.

a = [1, 2, 3, 4, 5]
b = [3, 4, 5, 1, 2]

result = []
for i in range(len(a)):
   if a[i] not in b:
      result.append(a[i])
   if  b[i] not in a:
      result.append(b[i])
        
print("The distinct elements are:", result)

Output

The distinct elements are: []

No distinct elements found in the given set of arrays.

Using Sets

Finding the distinct elements in two arrays is very similar to finding the symmetric difference between two sets. And by using Python Sets data structure and its properties we can easily identify the distinct elements from two arrays.

Example

Initially, we will convert the lists to the sets and then apply the symmetric difference property ^ between the two sets to get the distinct elements.

a = [1, 2, 3, 4, 5]
b = [3, 4, 5, 6, 7, 8]
result = list((set(a) ^ set(b)))
if result:
    print("The distinct elements are:", result)
else:
    print("No distinct elements present in two arrays")

Output

The distinct elements are: [1, 2, 6, 7, 8]

Also we can use set.symmetric_difference() method to find the distinct elements from two arrays. The symmetric_difference() method returns all the unique items present in given sets.

Syntax

set_A.symmetric_difference(set_B)

Example

Let’s see an example to get the distinct elements from two arrays.

a = [1, 2, 3, 4, 5]
b = [3, 4, 5, 6, 7, 8]

result = list(set(a).symmetric_difference(set(b)))

if result:
    print("The distinct elements are:", result)
else:
    print("No distinct elements present in two arrays")

Output

The distinct elements are: [1, 2, 6, 7, 8]

Here we have used the symmetric_difference() method to get the symmetric difference of A and B to the result variable. Then the unique element set is again converted to a list by using list() function.

Example

If no distinct element is found then the symmetric_difference() method will return the empty set.

a = [1, 2, 3, 4, 5]
b = [3, 4, 5, 1, 2]

result = list(set(a).symmetric_difference(set(b)))

if result:
    print("The distinct elements are:", result)
else:
    print("No distinct elements present in two arrays")

Output

No distinct elements present in two arrays

In the above example all elements are common elements. So that the symmetric_difference() method returned the empty set.

Updated on: 29-May-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements