Problem · Hash Table
First Unique ID in a Stream
Learn this problemProblem statement
Process a finite stream of operations while maintaining the earliest ID whose total occurrence count is exactly one.
[1, id]addsidto the stream.[2]queries the earliest currently unique ID.- Return one string per query: the decimal ID, or the literal string
nullwhen no ID is unique. - An ID leaves the unique order on its second occurrence and never rejoins after later occurrences.
Function
firstUniqueResults(operations: int[][]) → String[]Examples
Example 1
operations = [[1,2],[1,3],[2],[1,2],[2],[1,3],[2]]return = ["2","3","null"]ID 2 is initially first. Its second occurrence removes it, leaving 3. After 3 repeats, no unique ID remains.
Example 2
operations = [[2],[1,-5],[2],[1,-5],[2]]return = ["null","-5","null"]The first query is empty, then -5 is unique until its second occurrence.
Constraints
operationsis non-empty.- Every operation is exactly
[1, id]or[2]. - IDs are signed integers.