First Unique Number - Problem
Design a data structure that efficiently tracks the first unique number in a dynamic stream of integers. Imagine you're processing a continuous flow of numbers and need to quickly identify which number appears exactly once and comes first in the order they were received.

Your task is to implement the FirstUnique class with three key methods:

FirstUnique(int[] nums) - Initialize with an array of numbers
int showFirstUnique() - Return the first number that appears exactly once, or -1 if no such number exists
void add(int value) - Add a new number to the stream

The challenge is to maintain real-time efficiency as numbers are continuously added to the stream. This problem simulates real-world scenarios like finding the first unique visitor ID, tracking unique transactions, or identifying the first non-duplicate item in a processing queue.

Input & Output

example_1.py — Basic Operations
$ Input: FirstUnique([2, 3, 5]) showFirstUnique() → 2 add(5) showFirstUnique() → 2 add(2) showFirstUnique() → 3
Output: 2, 2, 3
💡 Note: Initially [2,3,5] all appear once, so first unique is 2. Adding 5 makes it [2,3,5,5], but 2 is still first unique. Adding 2 makes it [2,3,5,5,2], now 2 appears twice, so first unique is 3.
example_2.py — No Unique Elements
$ Input: FirstUnique([7, 7, 7, 7, 7, 7]) showFirstUnique() → -1 add(7) showFirstUnique() → -1 add(3) showFirstUnique() → 3
Output: -1, -1, 3
💡 Note: All elements are 7 (not unique), so return -1. Adding another 7 doesn't change this. Adding 3 gives us the first unique element.
example_3.py — Dynamic Stream
$ Input: FirstUnique([809]) showFirstUnique() → 809 add(809) showFirstUnique() → -1
Output: 809, -1
💡 Note: Initially 809 appears once. After adding 809 again, it appears twice and is no longer unique, so return -1.

Visualization

Tap to expand
🍽️ Restaurant Order Priority SystemDish Counter🍕 Pizza: 3 orders🍔 Burger: 1 order🥗 Salad: 2 orders🍝 Pasta: 1 order🌮 Taco: 1 orderPriority Queue (Unique Orders)🍕Removed🍔🥗🍝🌮PRIORITY!How It Works1. Track Frequency: Count how many times each dish is ordered2. Queue Management: Keep potentially unique dishes in order3. Lazy Cleanup: Remove non-unique dishes only when checking priority4. Efficient Service: First unique dish gets immediate priority💡 Key Insight: Combine frequency tracking with ordered queue for O(1) performance!
Understanding the Visualization
1
Order Tracking
Keep a count of each dish type and maintain a queue of potentially unique orders
2
New Order
When a new order arrives, update the count and add to queue if it's the first time
3
Find First Unique
Clean up the front of the queue by removing dishes that are no longer unique
4
Serve Priority
The dish at the front of the cleaned queue gets priority
Key Takeaway
🎯 Key Insight: The optimal solution brilliantly combines frequency counting (hash map) with order preservation (queue), using lazy cleanup to achieve O(1) amortized performance for real-time stream processing.

Time & Space Complexity

Time Complexity
⏱️
O(1)

Both add() and showFirstUnique() are O(1) amortized. Each element is added/removed from queue at most once

n
2n
Linear Growth
Space Complexity
O(n)

Hash map stores up to n unique numbers, queue stores at most n elements

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 108
  • 1 ≤ value ≤ 108
  • At most 50000 calls will be made to showFirstUnique and add combined
  • Follow-up: Could you implement this with O(1) time complexity for both showFirstUnique() and add()?
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
32.0K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen