Problem overview
The function first_repeat receives a vector of integers, nums. Reading it from left to right, it returns the first value that appears for the second time, or -1 if no value repeats. All numbers are between 0 and 10^9, so -1 can't be confused with a real answer.
For [3, 1, 4, 1, 3] the answer is 1. The reading goes 3, 1, 4, and then 1 again at index 3. The second 3 comes later, at index 4, so 3 loses even though it appeared first. For [2, 7, 9] nothing repeats and the answer is -1.
Approach: remember what you have seen
Comparing every pair of numbers would work but costs O(n²). A set of the values met so far makes each check fast. For every number, ask the set: have I seen this before? If yes, this is the second appearance, and it is the first one to happen in reading order, so return it. If no, add it to the set and move on.
The order of the two actions matters. Check first, then add. If the number were added before the check, it would always be found.
Step by step
- Create the empty set
seen, a mutableHashSet<i32>. - Loop over
&numswith the variablenum, reading from left to right. - Check the set. If
seen.contains(num), return*num, the value behind the reference. - Add the number with
seen.insert(*num), after the check and still inside the loop. - Return -1 after the loop: it ended without returning, so nothing repeated.
The solution in Rust
impl Solution {
fn first_repeat(nums: Vec<i32>) -> i32 {
let mut seen: HashSet<i32> = HashSet::new();
for num in &nums {
if seen.contains(num) {
return *num;
}
seen.insert(*num);
}
return -1;
}
}
The Rust details worth a closer look:
- References. Looping over
&numsmakesnuma&i32.containstakes a reference, sonumis passed as is. The function returns ani32and the set storesi32values, so both places use*numto read the integer behind the reference. - Early return. A
returninside the loop leaves the whole function at once, so the loop never reads the numbers after the first repeat. inserttells you too.insertreturnstruewhen the value was new andfalsewhen it was already present. The lesson writes the check and the insert as two steps to keep them clear.- Why -1 is safe. The values are never negative, so the sentinel return value can't be a real number from the vector.
Complexity
Time: O(n) on average. Each number causes one lookup and at most one insertion, each constant time on average.
Space: O(n). In the worst case, when nothing repeats, the set holds every number.
Tracing the example
With [3, 1, 4, 1, 3], the set starts empty. 3 is not in it, so it is added. The same happens for 1 and 4. The next number is 1, and the set already holds it, so the function returns 1 at once. The final 3 is never read.
Edge cases and common mistakes
- Inserting before checking. Every number would already be in the set when checked, and the function would return the first element for any input.
- Returning the first value that has a duplicate. That is a different question. In
[3, 1, 4, 1, 3]it would give 3, but the answer here is 1, the value whose second appearance comes first. - Immediate repeat.
[6, 6, 6, 6]returns 6 at index 1. - No repeat. The loop finishes and the function returns
-1.
Testing
[3, 1, 4, 1, 3]gives1.[2, 7, 9]gives-1.[6, 6, 6, 6]gives6.[1, 2, 3, 2, 1]gives2.
