Home  >  Article  >  Backend Development  >  Python program to find distinct elements from two arrays

Python program to find distinct elements from two arrays

WBOY
WBOYforward
2023-09-15 13:25:02817browse

Python program to find distinct elements from two arrays

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

Arrays in Python

Python has no specific data type to represent arrays. Instead, we can use List as an array.

[1, 4, 6, 5, 3]

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

Input and output scenarios

Suppose we have two arrays A and B with integer values. And the resulting array will have different elements than the two arrays.

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

Elements 1, 6, 4, and 9 are unique values ​​between the two arrays.

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

No distinct elements found in the given 2 arrays.

Use for loop

We will use a for loop for arrays with equal number of elements.

Example

In the following example, we will define a 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 find different elements by using for loop and if condition. Initially, the loop is iterated and verified if the element arr1[i] is not present in the array arr2, then if the element is a different element we append that element to the result variable. In the same way, we validate the second array element to the first array. and store the different elements in the resulting array.

Example

Let's use another set of arrays and find different 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 array set.

Use Collection

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

Example

First, we convert the list to a set 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]

We can also use the set.symmetry_difference() method to find different elements in two arrays. The symmetry_difference() method returns all unique items present in the given collection.

grammar

set_A.symmetric_difference(set_B)

Example

Let's see an example of getting different 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 use the symmetry_difference() method to get the symmetry difference of A and B to the result variable. Then use the list() function to convert the set of unique elements into a list again.

Example

If no different elements are found, the symmetry_difference() method will return an 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 public elements. In this way, the symmetry_difference() method returns the empty set.

The above is the detailed content of Python program to find distinct elements from two arrays. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete