Problem overview
The function firstRepeat receives a vector<int> named nums. Reading it from left to right, it returns the first value that appears for the second time, or -1 if no value appears twice. The vector has at least one number, and every number is between 0 and 1,000,000,000, so -1 can never be a real answer.
For [3, 1, 4, 1, 3] the answer is 1. Reading from the left you meet 3, 1, 4, and then 1 again, which is the first repeat. The second 3 comes later, so 3 is not the answer even though it appeared first. For [2, 7, 9] the answer is -1, and for [6, 6, 6, 6] it is 6, because the second 6 is already a repeat.
Approach: remember what you have seen
Scan the numbers in order and keep a set of the values met so far. For each number, ask whether it is already in the set. If it is, you have found a value for the second time, and because you read from the left, no earlier number could have been a repeat, or the function would have returned already. If it isn't, add it and move on. When the loop ends without a return, nothing repeated.
The alternative of comparing every number with every other takes O(n²) time. The set makes the "have I seen this?" question cheap.
Step by step
- Create
seen. An emptyunordered_set<int>: nothing has been read yet. - Loop over
numsin order. A range-basedforwith anintnamednumreads from left to right. - Check first.
seen.count(num)is 1 if the value is in the set and 0 if not, soseen.count(num) > 0means this value was met before. Return it. - Then add.
seen.insert(num)records the value for later turns. - Return -1 after the loop. No number was found twice.
The solution in C++
class Solution {
public:
int firstRepeat(vector<int>& nums) {
unordered_set<int> seen;
for (int num : nums) {
if (seen.count(num) > 0) {
return num;
}
seen.insert(num);
}
return -1;
}
};
The C++ details worth knowing:
- The order of the two statements matters. Check, then insert. If you inserted first, every number would be found in the set on its own turn, and the function would return the very first number.
counton a set is 0 or 1. A set holds each value at most once, socountworks as a membership test. Since C++20 there is alsocontains, but with C++17countis the way to ask.insertignores duplicates. Adding a value already in the set changes nothing. Here that never happens, because a known value has already caused a return.- An early
returnends the loop. The function stops at the first repeat and does not read the rest of the vector. - The sentinel -1 is safe. The task limits the numbers to 0 or more, so -1 can't be confused with a real repeated value.
Complexity
Time: O(n) on average. Each number costs one lookup and at most one insertion, both constant time on average. Space: O(n), because in the worst case, when nothing repeats, the set holds every number.
Edge cases and common mistakes
- Inserting before checking. The function would return
nums[0]every time. - Confusing the first value to repeat with the first repeat. In
[3, 1, 4, 1, 3], 3 is the first value to appear, but 1 is the first to appear again, and the answer is 1. - Several repeats. Only the first one found matters; the function returns on it.
- All the same.
[6, 6, 6, 6]returns 6 at index 1. - A single number.
[7]has nothing to repeat, so the answer is -1.
Testing
[3, 1, 4, 1, 3]gives 1.[2, 7, 9]gives -1.[6, 6, 6, 6]gives 6.
