1429. First Unique Number
Problem
Implement a data structure that finds the first unique number in a stream.
Design a data structure that supports:
FirstUnique(nums): Initialize with a stream of numbers.showFirstUnique(): Return the first unique number in the stream. If no unique number exists, return -1.add(value): Add a new number to the stream.
A unique number is one that appears exactly once in the stream.
Examples
Input: [["FirstUnique","showFirstUnique","add","add","showFirstUnique","add","showFirstUnique"],[[[2,3,5]],[],[3],[3],[],[2],[]]]
Output: [null,2,null,null,2,null,5]
Input: [["FirstUnique","showFirstUnique","add","add","showFirstUnique","add","showFirstUnique"],[[[2,3,5]],[],[3],[3],[],[2],[]]]
Output: [null,2,null,null,2,null,5]
Hints
Use a hash map to count frequency of each number.
Maintain a queue (or ordered data structure) to keep track of order.
In showFirstUnique, skip elements that are no longer unique.
Related Problems
1429. First Unique Number
Implement a data structure that finds the first unique number in a stream.