Loading...
Loading...
The college library logs every entry at the gate by the student's name. The librarian wants to spot first-time visitors. After each entry, she asks: among everyone who has come so far, who is the earliest student that has visited exactly once?
For each of the n entries, in order, print that student's name, or -1 if every student so far has come more than once.
Example 1
Input
6 ravi asha ravi kiran asha meena
Output
ravi ravi asha asha kiran kiran
After entry 3, ravi has come twice, so asha is the earliest single visitor. After entry 5, asha has also come twice, so kiran is the answer.
Example 2
Input
4 anu anu dev dev
Output
anu -1 dev -1
After entries 2 and 4, nobody has come exactly once.
Try the problem first. Open one hint at a time.
You need two things: how many times each name has come, and the order in which names first arrived.
Keep the counts in a HashMap and add each new name to the back of a queue.
The answer is at the front of the queue, but only after you throw away front names whose count is more than 1. A name thrown away can never be the answer again.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor