Set .difference() Operation Hacker Rank solution – Queslers

Problem : Set .difference() Operation HackerRank solution

Set .difference() Operation in python – HackerRank Solution

.difference()
The tool .difference() returns a set with all the elements from the set that are not in an iterable.
Sometimes the – operator is used in place of the .difference() tool, but it only operates on the set of elements in set.
Set is immutable to the .difference() operation (or the – operation).

>>> s = set("Hacker")
>>> print s.difference("Rank")
set(['c', 'r', 'e', 'H'])

>>> print s.difference(set(['R', 'a', 'n', 'k']))
set(['c', 'r', 'e', 'H'])

>>> print s.difference(['R', 'a', 'n', 'k'])
set(['c', 'r', 'e', 'H'])

>>> print s.difference(enumerate(['R', 'a', 'n', 'k']))
set(['a', 'c', 'r', 'e', 'H', 'k'])

>>> print s.difference({"Rank":1})
set(['a', 'c', 'e', 'H', 'k', 'r'])

>>> s - set("Rank")
set(['H', 'c', 'r', 'e'])

Task :

Students of District College have a subscription to English and French newspapers. Some students have subscribed to only the English newspaper, some have subscribed to only the French newspaper, and some have subscribed to both newspapers.
You are given two sets of student roll numbers. One set has subscribed to the English newspaper, and one set has subscribed to the French newspaper. Your task is to find the total number of students who have subscribed to only English newspapers.


Input Format :

The first line contains the number of students who have subscribed to the English newspaper.
The second line contains the space separated list of student roll numbers who have subscribed to the English newspaper.
The third line contains the number of students who have subscribed to the French newspaper.
The fourth line contains the space separated list of student roll numbers who have subscribed to the French newspaper.

Constraints :

  • 0 < Total number of student in college < 1000

Output Format :

Output the total number of students who are subscribed to the English newspaper only.


Sample Input :

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

Sample Output :

4

Explanation :

The roll numbers of students who only have English newspaper subscriptions are:
4, 5, 7 and 9.
Hence, the total is 4 students.

Set .difference() Operation Hacker Rank solution in python 2

eng = set()
fre = set()
n = raw_input()
for i in raw_input().split(' '):
  eng.add(i)
m = raw_input()
for i in raw_input().split(' '):
  fre.add(i)
sol = eng.difference(fre)
print len(sol)

Set .difference() Operation Hacker Rank solution in python 3

# Enter your code here. Read input from STDIN. Print output to STDOUT
n1 = int(input())
set_1 = set(map(int,input().split()))
n2 = int(input())
set_2 = set(map(int,input().split()))
print(len(set_1-set_2))

Set .difference() Operation Hacker Rank solution in pypy

# Enter your code here. Read input from STDIN. Print output to STDOUT
n = input()
s = set(map(int, raw_input().split())) 

m = input()
t = set(map(int, raw_input().split()))

print len(s.difference(t))

Set .difference() Operation Hacker Rank solution in pypy 3

# Enter your code here. Read input from STDIN. Print output to STDOUT
n1=input()
li1 = input().split()
s1 = set(li1)
n2=input()
li2 = input().split()
s2 = set(li2)
s1s2 = s1.difference(s2)
print(len(s1s2))
Set .difference() Operation Hacker Rank Solution Review:

In our experience, we suggest you solve this Set .difference() Operation Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Set .difference() Operation is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution

Set .difference() Operation Hacker Rank Solution Conclusion:

I hope this Set .difference() Operation Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Mini-Max Sum Hacker Rank Solution

String Validators Hacker Rank Solution

Text Alignment Hacker Rank solution

String validators Hacker Rank Solution

Staircase Hacker Rank Solution

Leave a Reply

Your email address will not be published.