Set .add() Problem

The problem at hand involves dealing with sets in Python. The user is expected to input a number of country names. These names are then added to a set. Since sets in Python do not allow duplicate entries, this effectively filters out any repeated country names. The program then prints out the number of unique countries that have been inputted.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Set .add() Problem HackerRank solution using CodeRankGPT:

The solution uses the built-in set data structure in Python. A set is an unordered collection of unique elements. When a country name is inputted, it is added to the set using the .add() method. If the country name is already in the set, it is not added again, ensuring that the set only contains unique country names. After all country names have been inputted, the len() function is used to count the number of elements in the set, which is then printed out. This gives us the number of unique country names.


n = int(input())
country_set = set()
for _ in range(n):
    country_name = input()
    country_set.add(country_name)
print(len(country_set))

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform