HackerRank Python Set Solutions
One of the best online resources for testing your coding skills is HackerRank – a platform that can be used to assess developer skills as part of preparation for interviews.
A large section of the site is dedicated to Python coding, with a subset of questions designed to test your knowledge of Python sets.
This guide walks you through a selection of coded solutions to the Python sets questions on HackerRank.
- Introduction to Sets
- Symmetric Difference
- Set .add()
- Set .discard(), .remove() & .pop()
- Set .union()
- Set .intersection()
- Set .difference()
- Set Mutations
- Check Subset
- The Captain’s Room
- Check Strict Superset
- No Idea!
Task 1: Introduction to Sets
This task provides a simple introduction to the key features that define a set. That is, they contain an unordered collection of distinct elements.
We need to create a function that performs three main steps:
- Accepts a collection of space-separated integers
- Converts the collection to a set
- Calculates the average of the set
You’ll find the full solution to the task below, followed by a breakdown of the steps taken to get there.
Solution 1:def average(array): dist = set(array) return sum(dist)/len(dist) if __name__ == '__main__': n = int(input()) arr = list(map(int, input().split())) result = average(arr) print(result)
In the code above, we only provided the code for lines 2 and 3. The remaining structure was already created, but we’ve included it to show the full solution.
Let’s break down the solution.
Input Format: The first line contains the integer, N, the total number of plants.
We can request this input from the user with the following code:
n = int(input())
Input Format: The second line contains the N space separated heights of the plants.
We can request this input from the user with the following code:
arr = list(map(int, input().split()))
This single line:
- Requests an input from the user
- Splits the input based on the default ‘ ‘ whitespace
- Creates a list of the items (default behaviour of
split()
) - Convert each list item to an
int
usingmap
- Assigns the resulting list to variable
arr
Output Format: Output the average height value on a single line.
The average
function now just needs to be passed the arr
list as a parameter. The list can then be converted to a set to remove duplicate values, then output the average height using:
def average(array): dist = set(array) return sum(dist)/len(dist)
So, let’s test if it works.
Input:10
161 182 161 154 176 170 167 171 170 174
Output:
169.375
Resource: View the problem on HackerRank
Task 2: Symmetric Difference
The Symmetric Difference task tests your knowledge of the symmetric_difference
method, which is unique to sets.
In the example: set_A.symmetric_difference(set_B)
, the result would be elements that are contained in either set_A
or set_B
, but not both.
m, m_list, n, n_list = int(input()), set(map(int, input().split())), int(input()), set(map(int, input().split())) srt = sorted(m_list.symmetric_difference(n_list)) for x in srt: print(x)
Input Format: The first line of input contains an integer, M.
We can request this input from the user with the following code:
m = int(input())
Input Format: The second line contains M space separated integers.
We can request this input from the user with the following code:
m_set = set(map(int, input().split()))
This code splits the collection of integers on the default whitespace character to create a list
of strings. The map()
function is used to convert these values to integers, then the list
of integers is passed to the set()
constructor to create a set
.
Input Format: The third line contains an integer, N.
We can request this input from the user with the following code:
n = int(input())
Input Format: The fourth line contains N space separated integers.
We can request this input from the user with the following code:
n_set = set(map(int, input().split()))
Output Format: Output the symmetric difference integers in ascending order, one per line.
So, we now have two sets: m_set
and n_set
. We simply need to find the symmetric difference between the two and sort the result.
This can be achieved using the following line:
srt = sorted(m_set.symmetric_difference(n_set))
Finally, we need to output the resulting set of integers, one per line.
for x in srt: print(x)
So, let’s see if this solution works.
Input:4
2 4 5 9
4
2 4 11 12
Output:
5
9
11
12
Resource: View the problem on HackerRank
Task 3: Set .add()
The Set .add() task tests your knowledge of the add()
operation, which is unique to sets.
Calling add(val)
on a set will add the val
to the collection.
set1 = set() for _ in range(int(input())): set1.add(input()) print(len(set1))
Input Format: The first line contains an integer N, the total number of country stamps.
We can request this input from the user with the following code:
int(input())
We only need this value to know how many times to request input of country names, so we can use this input as the range()
for the for
loop.
Input Format: The next N lines contains the name of the country where the stamp is from.
We’ll need to initialise the set
before the for
loop, if we want to add elements to it inside the loop.
We don’t need to access any iterator variable inside the loop, so we can use for _ in
instead of for x in
.
Putting this together and adding each country input to the set
using the add(val)
method gives us the following code:
set1 = set() for _ in range(int(input())): set1.add(input())
Output Format: Output the total number of distinct country stamps on a single line.
Because Python sets only contain distinct values, we simply have to print the len()
of set1
to achieve the desired output format.
print(len(set1))
So, let’s prove this solution works.
Input:7
UK
China
USA
France
New Zealand
UK
France
Output:
5
Resource: View the problem on HackerRank
Task 4: Set .discard(), .remove() & .pop()
This task tests your knowledge of the Python set operations that remove items from a set.
The .discard(val)
operation removes the val
and doesn’t raise a KeyError if the val
doesn’t exist. .remove(val)
will remove the val
if it exists, and raise a KeyError if it doesn’t, while .pop()
will remove an arbitrary element from the set.
You can find a full explanation of how these operations work, complete with code samples, in our Python sets guide.
Solution 1:n = int(input()) s = set(map(int, input().split())) N = int(input()) for _ in range(N): x = input().split() if len(x) == 1: s.pop() elif x[0] == 'remove': try: s.remove(int(x[1])) except: next elif x[0] == 'discard': s.discard(int(x[1])) print(sum(s))
Input Format: The first line contains integer n, the number of elements in the set s.
The n input isn’t used in this solution, but as it’s part of the Input Format, we read it in using the following code:
n = int(input())
Input Format: The second line contains n space separated elements of set s. All of the elements are non-negative integers, less than or equal to 9.
This second input will form the contents of our set
. As we’ve done before, we can split the input to get a list of strings, convert these to integers using map()
, then pass the list
to the set()
constructor.
s = set(map(int, input().split()))
Input Format: The third line contains integer N, the number of commands.
As with the first input, we just need to get an integer input from the user. But in this case, we’ll be passing N to the range()
function in our for
loop. This is so we know how many times to request input from the user for the commands.
N = int(input())
Input Format: The next N lines contains either pop, remove and/or discard commands followed by their associated value.
There are a number of potential solutions for this logic. One of the simplest can be found below.
Here, we’re prompting for command input N times in a for
loop. Each iteration, we’re splitting the input to create a list
, then determining which command to run based on the string value found at index 0.
for _ in range(N): x = input().split() if len(x) == 1: s.pop() elif x[0] == 'remove': try: s.remove(int(x[1])) except: next elif x[0] == 'discard': s.discard(int(x[1]))
Output Format: Print the sum of the elements of set s on a single line.
Now all that’s left to do is pass our set s to the built-in sum()
function and print the result.
print(sum(s))
We can check that the solution works using a set of test case inputs from HackerRank.
Input:9
1 2 3 4 5 6 7 8 9
10
pop
remove 9
discard 9
discard 8
remove 7
pop
discard 6
remove 5
pop
discard 5
Output:
4
Resource: View the problem on HackerRank
Task 5: Set .union()
The Set .union() task tests your knowledge of the union(iterables)
set operation.
This operation combines a set with values from one or more other iterables to produce a combined set of unique values.
Solution 1:n, set_n, b, set_b = int(input()), set(input().split()), int(input()), set(input().split()) print(len(set_n.union(set_b)))
Input Format: The first line contains an integer, n, the number of students who have subscribed to the English newspaper.
As seen in earlier tasks, the following code will convert user input to an int
:
n = int(input())
Input Format: The second line contains n space separated roll numbers of those students.
Being space separated means we can split()
the input on the default whitespace. The resulting list
can then be passed to the set()
constructor to create our set
.
There’s no need to map()
the list elements to integers, as the output for this task will simply be the len()
of the union
of set_a
and set_b
.
set_n = set(input().split())
Input Format: The third line contains b, the number of students who have subscribed to the French newspaper.
The following code can be used to retrieve this input. We don’t even need to take up memory by assigning inputs n and b to variables, as they’re not referenced in the solution. But for clarity, we’ll leave them assigned.
b = int(input())
Input Format: The fourth line contains b space separated roll numbers of those students.
Use the same code as when input was assigned to set_n
to populate set_b
:
set_b = set(input().split())
Output Format: Output the total number of students who have at least one subscription.
Output the len()
of the union()
between set_n
and set_b
:
print(len(set_n.union(set_b)))
Let’s confirm the solution works by using input from one of the HackerRank test cases.
Input:9
1 2 3 4 5 6 7 8 9
9
10 1 2 3 11 21 55 6 8
Output:
13
Resource: View the problem on HackerRank
Task 6: Set .intersection()
The Set .intersection() task tests your knowledge of the intersection(iterables)
set operation.
The solution is identical to the one used for Task 5, so we won’t repeat the step-by-step walkthrough. The only difference is the use of intersection(set_b)
instead of union(set_b)
.
n, set_n, b, set_b = int(input()), set(input().split()), int(input()), set(input().split()) print(len(set_n.intersection(set_b)))
Let’s confirm this solution works.
Input:9
1 2 3 4 5 6 7 8 9
9
10 1 2 3 11 21 55 6 8
Output:
5
Resource: View the problem on HackerRank
Task 7: Set .difference()
The Set .difference() task tests your knowledge of the difference(iterables)
set operation.
The solution is identical to the one used for Task 5, so we won’t repeat the step-by-step walkthrough. The only difference is the use of difference(set_b)
instead of union(set_b)
.
n, set_n, b, set_b = int(input()), set(input().split()), int(input()), set(input().split()) print(len(set_n.difference(set_b)))
Let’s confirm this solution works.
Input:9
1 2 3 4 5 6 7 8 9
9
10 1 2 3 11 21 55 6 8
Output:
4
Resource: View the problem on HackerRank
Task 8: Set Mutations
We’ve already seen how to add and remove set elements. Task 8 tests our knowledge of mutating a set, which is essentially updating a set based on the elements found in one or more other iterables.
This includes the update(iterables)
, intersection_update(iterables)
, difference_update(iterables)
and symmetric_difference_update(iterables)
operations.
n, set_A = int(input()), set(input().split()) for _ in range(int(input())): op, set_B = input().split(), input().split() if op[0] == 'intersection_update': set_A.intersection_update(set_B) elif op[0] == 'difference_update': set_A.difference_update(set_B) elif op[0] == 'symmetric_difference_update': set_A.symmetric_difference_update(set_B) elif op[0] == 'update': set_A.update(set_B) print(sum(map(int, set_A)))
Input Format: The first line contains the number of elements in set A.
The second line contains the space separated list of elements in set A.
We can request these first two inputs using the following line of code:
n, set_A = int(input()), set(input().split())
Input Format: The third line contains integer N, the number of other sets.
This value is only going to be used to limit how many times we loop and request the next 2 * N lines, so we don’t need to consume memory by assigning the value to a variable. We can simply pass the input directly to the range()
of our for
loop, as below:
for _ in range(int(input())):
Input Format: The next 2 * N lines are divided into N parts containing two lines each.
The first line of each part contains the space separated entries of the operation name and the length of the other set.
The second line of each part contains space separated list of elements in the other set.
So, we need to request these two inputs each time we iterate around our for
loop.
From the first line, we only need the first part, so we can split()
this input and get the operation name from index 0 of the resulting list.
For the second line, we need to split()
the given input on the default whitespace. This creates a list
, which is an iterable, so we can pass it directly to the set
operation. To save computation, rather than convert the string elements of the list to integers each time we loop, we’ll do it once on set_A
after the loop completes. This will allow us to calculate the sum()
for the output.
for _ in range(int(input())): op, set_B = input().split(), input().split() if op[0] == 'intersection_update': set_A.intersection_update(set_B) elif op[0] == 'difference_update': set_A.difference_update(set_B) elif op[0] == 'symmetric_difference_update': set_A.symmetric_difference_update(set_B) elif op[0] == 'update': set_A.update(set_B)
Output Format: Output the sum of elements in set A.
After all the operations have been performed on set_A
, we simply need to convert the elements to integers, then pass the object to the built-in sum()
function:
print(sum(map(int, set_A)))
Let’s confirm this solution produces the correct output.
Input:16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 24 52
4
intersection_update 10
2 3 5 6 8 9 1 4 7 11
update 2
55 66
symmetric_difference_update 5
22 7 35 62 58
difference_update 7
11 22 35 55 58 62 66
Output:
38
Resource: View the problem on HackerRank
Task 9: Check Subset
This task is designed to test your knowledge of the issubset()
method. Given T test cases, we need to print True
if the fourth line of the test case (set B) is a subset of the second line of the test case (set A).
for _ in range(int(input())): line_1, line_2 = int(input()), set(input().split()) line_3, line_4 = int(input()), set(input().split()) print(line_2.issubset(line_4))
Input Format: The first line will contain the number of test cases, T.
This number is only needed to control how many times we loop through the test cases. So, rather than consume memory by assigning the value to a variable, we can pass the input directly to the range()
in our for
loop. We can also use _
as a general purpose “throwaway” variable, to indicate that it is being deliberately ignored and not used referenced within the loop.
for _ in range(int(input())):
Input Format: The first line of each test case contains the number of elements in set A.
The second line of each test case contains the space separated elements of set A.
The third line of each test case contains the number of elements in set B.
The fourth line of each test case contains the space separated elements of set B.
We don’t need to use the values entered on the first and third line of each test case. All we need is the input from lines 2 and 4 to be split()
and passed to the set()
constructor.
line_1, line_2 = int(input()), set(input().split()) line_3, line_4 = int(input()), set(input().split())
Output Format: Output True or False for each test case on separate lines.
Because issubset()
returns a Boolean, we can simply print the result of
at each loop iteration.
print(line_2.issubset(line_4))
We can confirm the solution works using the sample input below.
Input:3
5
1 2 3 5 6
9
9 8 5 6 3 2 1 4 7
1
2
5
3 6 5 4 1
7
1 2 3 5 6 8 9
3
9 8 2
Output:
True
False
False
Resource: View the problem on HackerRank
Task 10: The Captain’s Room
The previous tasks have provided a clear direction for the set
methods you should be using to solve them. But Task 10 really only describes the problem, without providing any suggestions as to the methods that should be used to solve it.
This means there will be a wide range of potential solutions. Ours simply tries to perform all necessary logic in a single pass of the data and only creates variables when necessary to optimize memory allocation.
Solution 1:_ = input() set1, set2 = set(), set() for room in (input().split()): if room not in set1: set1.add(room) else: set2.add(room) set1.difference_update(set2) print(set1.pop())
Input Format: The first line consists of an integer, K, the size of each group.
Looking ahead to the Output Format of this task, input K isn’t important for this solution. Therefore, we can read the input to a throw away variable of _
.
_ = input()
Input Format: The second line contains the unordered elements of the room number list.
Reading through the problem, we know that the Captain’s room will only appear once in the list. So, if we build up two sets (set1
and set2
), then only add a room number to set2
if it’s already present in set1
, we can be sure that the Captain’s room will be the one that appears in set1
but not set2
.
As we need to loop and assign each room number to either set1
or set2
, we have to declare our sets before the loop starts.
set1, set2 = set(), set()
We can now loop through the input list of room numbers, and assign to set1
or set2
.
for room in (input().split()): if room not in set1: set1.add(room) else: set2.add(room)
Output Format: Output the Captain’s room number.
All that’s left now is to find which room number appears in set1
but not set2
. This can be achieved using the difference_update(iterables)
set operation.
set1.difference_update(set2)
We should now only have one item in set1
, which is the Captain’s room number. Calling pop()
on set1
will produce this as the output, which solves our problem.
print(set1.pop())
So, let’s check that this works.
Input:5
1 2 3 6 5 4 4 2 5 3 6 1 6 5 3 2 4 1 2 5 1 4 3 6 8 4 3 1 5 6 2
Output:
8
Resource: View the problem on HackerRank
Task 11: Check Strict Superset
This task is designed to test your knowledge of the issuperset()
method. Given set A, and n other sets, you need to print True
if set A is a strict superset of the N sets.
set_A, n = set(input().split()), int(input()) cnt = 0 check = True for _ in range(n): if not set_A.issuperset(set(input().split())): check = False break print(check)
Let’s break down the solution.
Input Format: The first line contains the space separated elements of set A.
We can read in a space separated string, split based on whitespace to get the individual elements, then pass this list
of elements to the set()
constructor using the code below:
set_A = set(input().split())
Input Format: The second line contains integer n, the number of other sets.
With the previous input there was no mention that the format had to be integers, so we left the elements as strings after the split()
. With this input, we need to convert to an int
, which we can do using:
n = int(input())
Input Format: The next n lines contain the space separated elements of the other sets.
For this, we need to loop and prompt the user for input n times. We’ve chosen to break
from the loop as soon as set A isn’t a strict superset of the user’s input.
This is because the Output Format states that set A must be a strict superset of ALL other sets entered by the user.
cnt = 0 check = True for _ in range(n): if not set_A.issuperset(set(input().split())): check = False break
Output Format: PrintTrue
if set A is a strict superset of all other N sets. Otherwise, printFalse
.
We initialised the check
variable as True
at the start. Unless we broke out of the loop, it should still be True
, so we can just print the check
variable.
print(check)
So, let’s test if it works.
Input:
1 2 3 4 5 6 7 8 9 10 11 12 23 45 84 78
2
1 2 3 4 5
100 11 12
Output:
False
Resource: View the problem on HackerRank
Task 12: No Idea!
As with Task 10, we’re given a problem without any prompts as to which Python set methods we should be using to solve it. We’ll start with a simple for
loop solution.
_ = input().split() arr, set_A, set_B = input().split(), set(input().split()), set(input().split()) happiness = 0 for x in arr: if x in set_A: happiness += 1 elif x in set_B: happiness -= 1 print(score)
Input Format: The first line contains integers n and m separated by a space.
In this solution, we’re not actually using the first input, so we can assign its value to a throw away variable of _
.
_ = input().split()
Input Format: The second line contains n, the elements of the array.
With the second input, we simply need to store the elements in an iterable. The sample input for the task is a single line of values, separated by whitespace. Therefore, we can use split()
to create a list
.
arr = input().split()
Input Format: The third and fourth lines contain m integers, A and B, respectively.
We want to pass these elements to the set
constructor to create distinct collections of elements to check against. There could be a significant performance impact if we left these as a list
after the split()
operation.
set_A, set_B = set(input().split()), set(input().split())
Output Format: Output a single integer, your total happiness.
The simplest solution now is to loop through the input from line 2, adding 1 to a variable if the element is in set_A
and subtracting 1 from the variable if the element is in set_B
.
It’s necessary to create the variable before it’s referenced in the loop, and assign an initial value of zero. For this solution, the variable is ‘happiness’.
happiness = 0 for x in arr: if x in set_A: happiness += 1 elif x in set_B: happiness -= 1
To output the resulting ‘happiness’ value, we simply need to pass the variable to the built-in print()
function.
print(score)
Now we can confirm this solution works, by passing in the values below as input.
Input:3 2
1 5 3
3 1
5 7
Output:
1
Resource: View the problem on HackerRank