Python Program to find the distinct elements from two arrays

In programming, an array is a data structure used to store a collection of homogeneous data elements. Each element is identified by a key or index value. In Python, we use lists to represent arrays.

Finding distinct elements from two arrays means identifying unique elements that exist in one array but not in the other this is also known as the symmetric difference.

Input Output Scenarios

Let's look at example inputs and expected outputs ?

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

The elements 1, 4, 6, 9 are unique to one array each.

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

No distinct elements exist since both arrays contain the same elements.

Using For Loop

We can iterate through both arrays and check which elements are unique to each ?

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)
The distinct elements are: [1, 6, 4, 9]

This approach works by checking each element in arr1 to see if it exists in arr2, and vice versa. Elements that don't exist in the other array are added to the result.

Example with No 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)
The distinct elements are: []

Using Sets with Symmetric Difference Operator

The most efficient approach uses Python sets and the symmetric difference operator ^ ?

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")
The distinct elements are: [1, 2, 6, 7, 8]

Using symmetric_difference() Method

The symmetric_difference() method provides the same functionality with clearer syntax ?

Syntax

set_A.symmetric_difference(set_B)

Example

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")
The distinct elements are: [1, 2, 6, 7, 8]

Example with No Distinct Elements

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")
No distinct elements present in two arrays

Comparison

Method Time Complexity Readability Best For
For Loop O(n²) Medium Small arrays, learning purposes
Set ^ Operator O(n) High Most cases, concise code
symmetric_difference() O(n) High Explicit method calls

Conclusion

Use the set-based approaches for better performance with O(n) time complexity. The symmetric difference operator ^ provides the most concise syntax, while symmetric_difference() offers clearer intent.

Updated on: 2026-03-27T06:47:48+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements