easyArrays & HashingPure DSA~15 min
Cart First Unique Item
A shopping cart UI wants to highlight the first product the shopper added exactly once — a gentle nudge toward items they might have meant to buy in bulk. Given the ordered list of product IDs added to the cart, find the first one that appears only a single time.
Problem
Given an array of integer product IDs in insertion order, return the first ID that occurs exactly once. If every ID repeats, return -1.
Input
An array ids of length n (1 ≤ n ≤ 10^5), each value in [0, 10^9].
Output
A single integer — the first non-repeating ID, or -1 if none exists.
Constraints
- 1 ≤ n ≤ 100,000
- 0 ≤ ids[i] ≤ 10^9
- Order of first appearance defines 'first'
Examples
Example 1
Input
ids = [7, 3, 7, 5, 3, 9]
Output
5
7 and 3 each appear twice. 5 appears once and is earlier in the array than 9, so 5 is returned.
Example 2
Input
ids = [2, 2, 4, 4]
Output
-1
Every ID repeats, so there is no first-unique element.