A wildlife research station operates a network of motion-triggered trail cameras and keeps one log per day. Because a camera can fire several times for the very same animal passing by, a species name may be repeated inside a single day's log, but it should only count once toward that day no matter how many times it triggered the camera. Across all logged days, find the species that was recorded on the greatest number of distinct days. If two or more species are tied for the most days, report the one whose name is alphabetically smallest.
n, the number of logged days.n lines each describe one day's log: an integer k followed by k space-separated species names recorded that day (a name may repeat within the same day's list).Print a single line containing the species name that was recorded on the most distinct days (after removing duplicate entries within each day). Break any tie by printing the alphabetically smallest name among the tied species.
k over all days is at most 2 * 10^5Example 1
Input
3 3 fox owl fox 2 owl deer 1 fox
Expected
fox
Explanation
Day 1's entries {fox, owl, fox} dedupe to {fox, owl}; day 2's {owl, deer} stay as is; day 3 contributes {fox}. Totals: fox appears on 2 days, owl on 2 days, deer on 1 day. fox and owl tie at 2 days, and "fox" is alphabetically smaller than "owl", so the answer is fox.
Example 2
Input
2 2 cat dog 2 dog cat
Expected
cat
Explanation
Day 1 dedupes to {cat, dog}; day 2 dedupes to {dog, cat} (the same set). Totals: cat=2 days, dog=2 days. They tie at 2 days, and "cat" is alphabetically smaller than "dog", so the answer is cat.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →