Site icon Console Flare Blog

Set .intersection() Operation in Python – Complete HackerRank Solution Explained Step-by-Step

Set .intersection() Operation in Python - Complete HackerRank Solution Explained Step-by-Step

Set .intersection() Operation in Python – Complete HackerRank Solution Explained Step-by-Step

While solving Python problems on HackerRank, you’ll likely find the Set .intersection() in Python question.
It’s a quick challenge that shows how to find what’s common between two sets – a trick that’s surprisingly useful in real projects.

Beginners who wish to learn how Python sets operate, particularly how to quickly identify the common elements between two sets, will love this exercise.

In this comprehensive guide, you will discover:

By the end, you will be able to use Python’s Set .intersection() Operation with confidence in both coding challenges and practical settings.


Knowing Python Sets

In Python, an unordered collection of distinct elements is called a set. In addition to supporting mathematical operations like intersection, difference, and union, it automatically eliminates duplicates.

Example:

nums = {1, 2, 3, 3, 4}
print(nums)

Output:

{1, 2, 3, 4}

The advantage of using sets is that the duplicate 3 vanishes!


What Does Set.intersection() Operation in Python Do?

Elements that are present in both sets are found using Python’s Set.intersection() operation.

As an example:

A = {1, 2, 3, 4}
B = {3, 4, 5}
print(A.intersection(B)

Output:

{3, 4}

The & operator is another option:

print(A & B)

The set of shared elements is the outcome of both approaches.


The HackerRank Challenge: Using the Set.intersection() Function

There are two student groups involved in this HackerRank problem:

  1. One group buys the English newspaper.
  2. The other group has a subscription to the newspaper in France.
  3. A few students study both newspapers.
    It is your responsibility to determine the number of students in each group.

The format of input

  1. Number of subscribers to English newspapers in the first line
  2. Second line: English readers’ roll numbers
  3. Third line → the quantity of French newspaper readers
  4. Fourth line: French readers’ roll numbers

Example Input

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

Example Output

5

A description

The roll numbers of the students who read both newspapers are {1, 2, 3, 6, 8}.
As a result, five students subscribe to both.


Python Solution for Set .intersection() Operation

# Input number of English newspaper subscribers
n = int(input())
eng_students = set(input().split())

# Input number of French newspaper subscribers
m = int(input())
fre_students = set(input().split())

# Number of students who read both newspapers

print(len(eng_students.intersection(fre_students)))


A Step-by-Step Guide

Read Input Values

The first and third lines show the total number of subscribers.
They aren’t used directly later, but HackerRank needs them for its input format.

  1. Create Sets
    Space-separated roll numbers are converted into a set using set(input().split()), which automatically eliminates duplicates.

  2. Find Common Elements
    The key line:

    eng_students.intersection(fre_students)
    

    Only students who are present in both sets are included in the new set that is returned.

    Count the Similar Components
    The total number of shared roll numbers can be found using len().


Using & Operator Alternatives

Another way to make your code shorter is to use:

print(len(eng_students & fre_students))

The Python problem of the Set.intersection() Operation is correctly resolved by both methods.


Important Idea: Intersection Gives Back a Set

The original sets are unaltered by the intersection() method.
It generates a fresh set with only elements that are shared.

Example:

english = {"A", "B", "C", "D"}

french = {"C", "D", "E"} 
print(english.intersection(french))

Output:

{'C', 'D'}

Why This Idea Is Important

Python’s Set .intersection() operation is very helpful in:

Finding overlapping entries in datasets through data analysis

Database operations: to find records that match

Email marketing: identifying users who show up in several subscriber lists

Competitive programming enables fast and memory-efficient comparisons.

Understanding intersections makes it much easier to use other set operations like union() and difference().


Avoid these common mistakes.


Other Set Operations to Investigate


Final Thoughts

In Python, the Set.intersection() operation is straightforward but incredibly useful.
It facilitates the rapid and clean identification of shared data between two sources, which is a crucial ability for data-driven applications and coding interviews alike.

We applied this idea in the HackerRank problem to identify students who read both French and English newspapers.
However, the same reasoning holds true for any circumstance in which you need to identify what is common, whether it be customers, goods, tags, or anything else!

To improve your Python skills, keep working on related HackerRank problems.
Every little idea like this lays the groundwork for more difficult programming problems.

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.

YouTube Video:-


Do you want to learn more? Here are a few places to visit:

Python Sets Documentation

Related Post:

Console Flare

Exit mobile version