Site icon Console Flare Blog

Python set difference() Full Explanation with HackerRank Solution

Python set difference() Explained with Full Step-by-Step Solution

Python set difference() Explained with Full Step-by-Step Solution

Python set difference() allows you to compare two sets and find the elements that are in one set but not in the other. This guide explains how Python set difference() works, why it is useful, and how beginners can apply it to solve coding challenges with clear examples.

At District College, students subscribe to English and French newspapers. Some subscribe to both, while others subscribe to only one. You are given two lists of roll numbers: one for English subscribers and one for French subscribers. Your task is to find the number of students who subscribe only to the English newspaper.

In this post, we will cover the problem in detail, explain the solution concept, provide a complete Python implementation, and walk through the code line by line. You will also learn about time complexity, common mistakes, and sample test cases for practice.


Problem statement

Input format

Output format

Example

Input

9
1 2 3 4 5 6 7 8 9
9
10 1 2 3 11 21 55 6 8

Output

4

Explanation
English-only roll numbers are 4, 5, 7, 9. There are four of them.


Key idea

Why sets?


Python set difference solution

# read number of English subscribers (not used directly)
n = int(input())

# read English roll numbers and make a set
s1 = set(input().split())

# read number of French subscribers (not used directly)
n2 = int(input())

# read French roll numbers and make a set
s2 = set(input().split())

# print number of students who subscribed only to English
print(len(s1.difference(s2)))


Line-by-line explanation of Python set difference

Alternate syntax: s1 - s2 gives the same result as s1.difference(s2) for sets. Use either for clarity.


  1. No overlap
    1. Input 
3
1 2 3
2
4 5

1. Output

3
  1. All overlap
    2. Input
4
1 2 3 4
4
1 2 3 4

2. Output

0
  1. Duplicates in input lines
    3. Input
6
1 2 2 3 3 4
3
3 4 5

3. Output

2

Explanation: English set becomes {1,2,3,4}. After removing {3,4,5}, only {1,2} remain.


Common mistakes and tips


Learn more about Python’s official documentation on Python.org.

Visit YouTube to View the Complete Solution

Check out my YouTube tutorial, where I run and walk through this exact code in real time, if you want a step-by-step video explanation.

Related Post:

Integers Come in All Sizes HackerRank Python Solution
Power Function HackerRank Solution Explained in 5 Simple Steps

Final notes

Conclusion

Data science is transforming finance, from predictive analytics to personalised services. It’s not just a technology upgrade – it’s changing how the industry operates. Those who leverage data science will stay ahead in this data-driven era.

Want to explore more? Join our free Masters in Data Science with Power BI workshop at ConsoleFlare. Learn each tool and technology from scratch, understand the field deeply, and become job-ready for data science roles.

Why ConsoleFlare?

Register now, and our team will call you back to get you started.

Console Flare

Exit mobile version