We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Okay. As such, the only built-in used here, is "set". set() function forms a mathematical type of set and removes all the duplicate elements. Similarly, set.intersection() takes intersection of all the sets (as in mathematical context). Please have a look at the docs and tutorial for better understanding.
The logic is to form a set corresponding to each "rock" by removing the duplicate "gems". So, each set will have every gem present in rock, exactly once. Now, we just need to find the common gems in each set, which is basically equivalent to finding intersection of all sets mathematically. Thus, set.intersection gives the list of the "gems" present in each rock. The result just requires the count of gems, hence, length of above result is printed as answer.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Gemstones
You are viewing a single comment's thread. Return to all comments →
Okay. As such, the only built-in used here, is "set". set() function forms a mathematical type of set and removes all the duplicate elements. Similarly, set.intersection() takes intersection of all the sets (as in mathematical context). Please have a look at the docs and tutorial for better understanding.
The logic is to form a set corresponding to each "rock" by removing the duplicate "gems". So, each set will have every gem present in rock, exactly once. Now, we just need to find the common gems in each set, which is basically equivalent to finding intersection of all sets mathematically. Thus, set.intersection gives the list of the "gems" present in each rock. The result just requires the count of gems, hence, length of above result is printed as answer.