# HackerRank Solution: Python Symmetric Difference [3 Methods]

Author: Bashir Alam

## Question: Symmetric Difference [Python Sets]

Givenย 2ย sets of integers,ย Mย andย N, print their symmetric difference in ascending order. The termย symmetric differenceย indicates those values that exist in eitherย Mย orย Nย but do not exist in both.

Input Format

The first line of input contains an integer,ย M.
The second line containsย Mย space-separated integers.
The third line contains an integer,ย N.
The fourth line containsย Nย space-separated integers.

Output Format

Output the symmetric difference integers in ascending order, one per line.

Sample Input

`STDIN       Function-----       --------4           set a size M = 42 4 5 9     a = {2, 4, 5, 9}4           set b size N = 42 4 11 12   b = {2, 4, 11, 12}`

Sample Output

`591112`

If you are new to python then I would recommend reading out Python Sets and Python Symmetric Difference before attempting this question.

## Possible Solutions

### 1. Using Set Operations

In this solution we will read the number of elements in both the sets. The this code will next compute the symmetric difference between the two sets using the `symmetric_difference` method, which will find element which are in either of the two sets but not in both. Finally, we will print the elements of the symmetric difference in the ascending order. This approach uses Python's built-in set operations for simplicity and clarity.

``````# Step 1: Read the number of elements in the first set
M = int(input())

# Step 2: Read the elements of the first set and convert them to a set of integers
set1 = set(map(int, input().split()))

# Step 3: Read the number of elements in the second set
N = int(input())

# Step 4: Read the elements of the second set and convert them to a set of integers
set2 = set(map(int, input().split()))

# Step 5: Calculate the symmetric difference between the two sets
# Symmetric difference means elements that are in either set1 or set2 but not in both
symmetric_difference = set1.symmetric_difference(set2)

# Step 6: Print the symmetric difference elements in ascending order
for number in sorted(symmetric_difference):
print(number)``````

### 2. Using List Comprehension

In this solution, we begin by reading the number of elements in the first set and then the elements themselves, converting them into a set of integers. We do the same for the second set. Next, we use list comprehension to manually compute the symmetric difference by combining both sets and filtering out the common elements. Specifically, we create a new list containing elements that are in either set but not in both. Finally, we sort this list and print the elements in ascending order. This method explicitly demonstrates the logic behind finding the symmetric difference using list comprehension.

``````# Step 1: Read the number of elements in the first set
M = int(input())

# Step 2: Read the elements of the first set and convert them to a set of integers
set1 = set(map(int, input().split()))

# Step 3: Read the number of elements in the second set
N = int(input())

# Step 4: Read the elements of the second set and convert them to a set of integers
set2 = set(map(int, input().split()))

# Step 5: Calculate the symmetric difference using list comprehension
# This involves combining the two sets, then excluding elements found in both sets
symmetric_difference = sorted([x for x in set1.union(set2) if x not in set1.intersection(set2)])

# Step 6: Print the symmetric difference elements in ascending order
for number in symmetric_difference:
print(number)``````

### 3. Using Symmetric Difference Operator

In this solution, we start by reading the number of elements in the first set, followed by the elements themselves, which are converted into a set of integers. We repeat the process for the second set. We then calculate the symmetric difference using the `^` operator, which efficiently finds elements present in either set but not in both. This operator is a concise way to achieve the symmetric difference. Finally, we sort the resulting set and print the elements in ascending order. This approach leverages the succinct `^` operator for clarity and efficiency.

``````# Step 1: Read the number of elements in the first set
M = int(input())

# Step 2: Read the elements of the first set and convert them to a set of integers
set1 = set(map(int, input().split()))

# Step 3: Read the number of elements in the second set
N = int(input())

# Step 4: Read the elements of the second set and convert them to a set of integers
set2 = set(map(int, input().split()))

# Step 5: Calculate the symmetric difference using the ^ operator
# The ^ operator gives the symmetric difference, which is the same as using symmetric_difference method
symmetric_difference = set1 ^ set2

# Step 6: Print the symmetric difference elements in ascending order
for number in sorted(symmetric_difference):
print(number)``````

When we run the code from all three possible solutions on Hacker Rank, both the sample Test case are shown as successful:

## Summary

In this tutorial, we provided multiple solutions for a HackerRank question on Python sets to identify the symmetric difference between two sets of integers. The first solution uses the `symmetric_difference` method for a straightforward approach. The second solution employs list comprehension to explicitly demonstrate the logic behind symmetric difference computation. The third solution leverages the `^` operator for a concise and efficient calculation. Each method highlights different techniques to achieve the same goal, showcasing the versatility of Python's set operations.

Views: 57

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com