What every supplier carries
- O(n log n) · Hard
- Full plan
- Python
- JavaScript
- sets
- lists
- sorting
Problem
Don Ramiro stocks his hardware store from several suppliers, and each one leaves him a catalog. Before placing the order he wants to see which items he can buy from any of them: those are the ones that will never be missing from the counter.
Write a function that takes catalogs, a list where each element is one supplier's catalog: a list of items written in lowercase and with no accents. A catalog may repeat an item and may come empty. Return the list of the items that are in every catalog, in alphabetical order and with no repeats.
With three catalogs, ["tape", "hammer", "nail"], ["nail", "tape", "brush"] and ["tape", "nail", "drill"], the nail and the tape are in all three, and the hammer, the brush and the drill are in only one: you return ["nail", "tape"].
If no item is in every catalog, or if no catalog arrived at all, you return an empty list.
Examples
The example
[["tape", "hammer", "nail"], ["nail", "tape", "brush"], ["tape", "nail", "drill"]] → ["nail", "tape"]
Nothing in common
[["nail", "sandpaper"], ["brush", "cable"]] → []
A single catalog
[["drill", "brush", "drill"]] → ["brush", "drill"]
An item repeated in the same catalog
[["tape", "tape", "nail"], ["nail", "tape"]] → ["nail", "tape"]
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def common_stock(catalogs):
passJavaScript
function commonStock(catalogs) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.