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

  • First line: number of students who subscribed to the English newspaper (n)
  • Second line: n space-separated roll numbers for English subscribers
  • Third line: number of students who subscribed to the French newspaper (m)
  • Fourth line: m space-separated roll numbers for French subscribers

Output format

  • A single integer, the number of students who have subscribed only to the English newspaper

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

  • Use Python sets to remove duplicates and to run set operations quickly.
  • The set difference operation returns items in one set that are not in another.
  • If the English set is E and the French set isF, then E.difference(F) gives students who subscribe only to English.
  • The result size is len(E.difference(F)).

Why sets?

  • Membership checks in sets are fast.
  • Sets remove repeated roll numbers automatically.
  • Set operations are simple and expressive.

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)))

Set Difference HackerRank Solution


Line-by-line explanation of Python set difference

  • n = int(input())
    • Reads the first line. It is the count of English subscribers. We parse it to an int. The program does not require this number later, but the input includes it. Reading it keeps the input alignment correct.
  • s1 = set(input().split())
    • Reads the second line. input().split() Returns a list of roll-number strings separated by space.
    • set(...) converts the list to a set. This removes duplicates and gives O(1) average membership checks.
  • n2 = int(input())
    • Reads the third line, the count of French subscribers. It is parsed to int. It is not used later, but it moves input reading to the correct place.
  • s2 = set(input().split())
    • Reads the fourth line and converts it to a set for the same reasons.
  • print(len(s1.difference(s2)))
    • s1.difference(s2) Returns a new set containing elements  s1 that are not in s2.
    • len(...) gives the number of such elements.
    • print(...) outputs the final answer.

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


  • Small tests you can try for Python set difference
  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

  • Treat roll numbers as strings if they include leading zeros. Converting to int will drop leading zeros. Use split() and leave values as strings if roll numbers may have leading zeros.
  • Do not assume the line counts must match the actual number of items. Always read the count lines to keep input alignment.
  • Using lists only will make membership checks slow for large inputs. Use sets for speed.
  • s1.difference(s2) Returns a new set. It does not change s1. If you want to update s1 in place, use s1.difference_update(s2).

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

  • The solution is short and fast.
  • Sets give clean semantics for subscriptions and duplicates.
  • Use len(s1.difference(s2)) or len(s1 - s2) to get the final count.

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?

  • Recognised as one of the Top 10 Most Promising Data Science Training Institutes 2023

  • Learn in Hindi, in a way that’s easy to grasp

  • Curriculum focused on what you need to learn, nothing extra

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

Console Flare

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top