HackerRank Set Union Python Solution Explained Step by Step

HackerRank Set Union Python solution explained with code example

HackerRank Set Union Python Solution Explained Step by Step

The HackerRank Set Union Python Solution might look simple at first, but it’s a great example to understand how sets work in Python.
If you’re learning Python or preparing for coding interviews, this challenge helps you practice real problem-solving logic.

  1. The Meaning of the Question

    Some students read the English paper. Some people read the French one. Some people read both.
    You need to find out how many students read at least one paper.

    If a student is on both lists, count them only once.
    That’s all there is. Not hard, right?

    So, in short, we combine two sets.

  1. Before Coding: What Is the Function of.union()?

In Python, a set can only contain unique values. The union() function joins two sets and automatically eliminates duplicates.

Example:

A = {1, 2, 3}

B = {3, 4, 5}

print(A.union(B))

Output:

{1, 2, 3, 4, 5}

Clean results, no duplicates.

Input Format for the HackerRank Set Union Python Solution

Four lines of input will be given to you:

  1. Number of subscribers to English newspapers
  2. Number of subscribers to English newspapers
  3. The quantity of French newspaper readers
  4. Subscriber rolls for French newspapers

Printing the number of distinct students who read at least one newspaper is your aim.

  1. Full Code: HackerRank Set Union Python Solution

n = int(input())

s1 = set(input().split())

m = int(input())

s2 = set(input().split())

print(len(s1.union(s2)))

HackerRank Set Union Python Solution code

Now let’s go through it line by line.

  1. Line-by-Line Explanation

Line 1:

n = int(input())

Reads the number of subscribers in English.
Although HackerRank requires it for input format, we don’t use it afterwards.

Line 2:

s1 = set(input().split())

Takes English roll numbers.
.split() separates the numbers by spaces.
set() removes duplicates.

Line 3:

m = int(input())

French subscribers face the same issue.

Line 4:

s2 = set(input().split())

keeps a set of French roll numbers in storage.

Line 5:

print(len(s1.union(s2)))

.union() joins both sets.

The number of unique roll numbers that are left is counted by len().

Example Run of HackerRank Set Union Python Solution

Input:

9

1 2 3 4 5 6 7 8 9

9

10 1 2 3 11 21 55 6 8

Output:

13

A total of 13 pupils read at least one newspaper.

In a Brief Summary

What were we doing, then?

Review both lists.
created sets out of them.
Merged using.union()
Len() was used to count the number of distinct numbers.

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 Link: https://youtu.be/rycvi0PD2-0?si=xSmh4ZMzltsqQEWj


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

Python Sets Documentation

Related Post:

  • HackerRank Collections.deque() Solution in Python
  • HackerRank Python Solution – Using itertools combinations_with_replacement | In 5 Easy Steps

    Conclusion:

    The fusion of data science in the finance sector is not just a technological evolution but also a fundamental shift in the way the financial industry operates. From predictive analytics to personalized financial services, the applications of data science are reshaping traditional practices and opening up new possibilities. As we all move forward, the synergy between finance and data science will continue to evolve, creating a more robust, efficient, and resilient financial ecosystem. In this data-driven era, those who embrace the power of data science will be at the forefront of innovations and success in the world of finance.

    Want to know what else can be done by Data Science?

    If you wish to learn more about data science or want to advance your career in the data science field, feel free to join our free workshop on Master’s in Data Science with Power BI, where you will get to know how exactly the data science field works and why companies are ready to pay handsome salaries in this field.

    In this workshop, you will get to know each tool and technology from scratch, which will make you skillfully eligible for any data science profile.

    To join this workshop, register yourself on ConsoleFlare, and we will call you back.

    Thinking, Why Console Flare?

    Recently, ConsoleFlare has been recognized as one of the Top 10 Most Promising Data Science Training Institutes of 2023.

    Console Flare offers the opportunity to learn Data Science in Hindi, just like how you speak daily.
    Console Flare believes in the idea of “What to learn and what not to learn,” and this can be seen in their curriculum structure. They have designed their program based on what you need to learn for data science and nothing else.

    Want more reasons?
    Register yourself on ConsoleFlare, and we will call you back.
    Log in or sign up to view
    See posts, photos, and more on Facebook.
Console Flare

Leave a Reply

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

Back To Top