1429. First Unique Number
Design a data structure that supports:
Solution(nums): Initialize the stream withnums.showFirstUnique(): Return the first number in the stream that appears exactly once, or-1if none exists.add(value): Appendvalueto the stream.
A number is unique if its total frequency in the stream is exactly one.
Examples
Input: [["Solution","showFirstUnique","add","showFirstUnique","add","showFirstUnique","add","showFirstUnique"],[[[1,2,1,3,10]],[],[4],[],[2],[],[5],[]]]
Output: [null,2,null,2,null,3,null,3]
Input: [["Solution","showFirstUnique","add","showFirstUnique","add","showFirstUnique","add","showFirstUnique"],[[[2,3,2,4,11]],[],[5],[],[3],[],[6],[]]]
Output: [null,3,null,3,null,4,null,4]
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
Design a data structure that supports: