Problem overview
The method firstRepeat receives an array 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 appears twice. The numbers are between 0 and 1,000,000,000, so -1 can never be an element of the input.
For [3, 1, 4, 1, 3] the answer is 1. Reading from the left gives 3, 1, 4, and then 1 again, at index 3. That happens before the second 3, at index 4, so the answer is 1, even though 3 showed up first. For [2, 7, 9] nothing repeats and the answer is -1. For [6, 6, 6, 6] the second 6, at index 1, is already a repeat, so the answer is 6.
Approach
Remember the values already read in a set. Each new number then needs only one question: is it in the set?
- If yes, this is its second appearance and no earlier number repeated, so return it.
- If no, add it to the set and move on.
Reaching the end of the loop means no value was ever seen twice, so the method returns -1.
Checking membership in a set is fast, so the method makes a single pass. The slow alternative compares each number with all the ones before it, which takes O(n²) time.
Step by step
- Create the empty set.
seenholds the values met so far. It starts empty because nothing has been read yet. - Read left to right. A
for...ofloop overnumsvisits the numbers in order, which is what "first" depends on. - Check first. If
seen.has(num)is true, returnnum. - Then add.
seen.add(num)records the number, still inside the loop. Adding before checking would find every number already in the set, even on its first appearance. - Return -1 after the loop.
The solution in TypeScript
class Solution {
firstRepeat(nums: number[]): number {
let seen: Set<number> = new Set();
for (let num of nums) {
if (seen.has(num)) {
return num;
}
seen.add(num);
}
return -1;
}
}
Details worth knowing:
- The type of the variable. A set of numbers is written
Set<number>. The annotation is erased when TypeScript compiles to JavaScript. In code of your own you can also writeconst seen = new Set<number>();. hasandadd.seen.has(num)returnstrueorfalse.seen.add(num)inserts the value, and does nothing if it is already there.- Returning inside the loop. The first repeat found ends the method at once, so the rest of the array is never read.
- Why -1 is safe. The numbers are never negative, so the answer -1 can't be confused with a real repeated value. A value of 0 is a valid answer:
[0, 5, 0]returns 0.
Complexity
Time: O(n). Each number takes one has and at most one add, both constant time on average.
Space: O(n). In the worst case, when nothing repeats, the set holds every number.
Edge cases and common mistakes
- Adding before checking. The method would return the first number of the array every time.
- Returning the value that appeared first. In
[3, 1, 4, 1, 3]the answer is 1, the value whose second appearance comes first, not 3. - Treating 0 as "not found". Only -1 means that nothing repeats. A returned 0 is a real value.
- One number.
[7]has no repeat, so the loop ends and -1 is returned. - Forgetting the final
return -1. The method would returnundefinedwhen nothing repeats.
Testing
[3, 1, 4, 1, 3]gives1.[2, 7, 9]gives-1.[6, 6, 6, 6]gives6.[0, 5, 0]gives0.
