Python Program to Print uncommon elements from two sorted arrays
Last Updated :
18 Apr, 2023
Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order.
Examples :
Input : arr1[] = {10, 20, 30}
arr2[] = {20, 25, 30, 40, 50}
Output : 10 25 40 50
We do not print 20 and 30 as these
elements are present in both arrays.
Input : arr1[] = {10, 20, 30}
arr2[] = {40, 50}
Output : 10 20 30 40 50
Method 1:
The idea is based on the merge process of merge sort. We traverse both arrays and skip common elements.
Python3
# Python 3 program to find uncommon
# elements of two sorted arrays
def printUncommon(arr1, arr2, n1, n2):
i = 0
j = 0
k = 0
while (i < n1 and j < n2):
# If not common, print smaller
if (arr1[i] < arr2[j]):
print(arr1[i], end=" ")
i = i + 1
k = k + 1
elif (arr2[j] < arr1[i]):
print(arr2[j], end=" ")
k = k + 1
j = j + 1
# Skip common element
else:
i = i + 1
j = j + 1
# printing remaining elements
while (i < n1):
print(arr1[i], end=" ")
i = i + 1
k = k + 1
while (j < n2):
print(arr2[j], end=" ")
j = j + 1
k = k + 1
# Driver code
arr1 = [10, 20, 30]
arr2 = [20, 25, 30, 40, 50]
n1 = len(arr1)
n2 = len(arr2)
printUncommon(arr1, arr2, n1, n2)
Time Complexity: O(n1 + n2), where n1 and n2 represents the size of the given two arrays.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Method 2: Using Counter() function
Python3
from collections import Counter
# Python 3 program to find uncommon
# elements of two sorted arrays
def printUncommon(arr1, arr2):
frequency_arr1 = Counter(arr1)
frequency_arr2 = Counter(arr2)
result = []
for key in frequency_arr1:
if key not in frequency_arr2:
result.append(key)
for key in frequency_arr2:
if key not in frequency_arr1:
result.append(key)
# Sorting the result
result.sort()
for i in result:
print(i, end=' ')
# Driver code
arr1 = [10, 20, 30]
arr2 = [20, 25, 30, 40, 50]
printUncommon(arr1, arr2)
Please refer complete article on Print uncommon elements from two sorted arrays for more details!
Time Complexity: O(n*logn), as sort() function is used.
Auxiliary Space : O(n), where n is length of result list.
Method 3: using operator.countOf() method
Python3
# Python 3 program to find uncommon
# elements of two sorted arrays
import operator as op
def printUncommon(arr1, arr2):
result = []
for key in arr1:
if op.countOf(arr2, key) == 0:
result.append(key)
for key in arr2:
if op.countOf(arr1, key) == 0:
result.append(key)
# Sorting the result
result.sort()
for i in result:
print(i, end=' ')
# Driver code
arr1 = [10, 20, 30]
arr2 = [20, 25, 30, 40, 50]
printUncommon(arr1, arr2)
Time Complexity: O(NLogN)
Auxiliary Space : O(N)
Similar Reads
Javascript Program to Print uncommon elements from two sorted arrays Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples :Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50}Output : 10 25 40 50We do not print 20 and 30 as theseelements are pr
2 min read
Print uncommon elements from two sorted arrays Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements ar
6 min read
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
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
Program to print all distinct elements of a given integer array in Python | Ordered Dictionary Given an integer array, print all distinct elements in array. The given array may contain duplicates and the output should print every element only once. The given array is not sorted. Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45} Output: 12, 10, 9, 45, 2 Input: arr[] = {1, 2, 3, 4, 5} Out
2 min read
Python Set difference to find lost element from a duplicated array 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,
1 min read