Problem overview
The method firstRepeat receives a list of integers, nums. Reading the list 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 never negative, so -1 can't be mistaken for a real answer.
For [3, 1, 4, 1, 3] the answer is 1. Reading from the left gives 3, 1, 4, and then 1 again, so the second 1, at index 3, is the first repeat. The second 3, at index 4, comes later, even though 3 showed up first. For [2, 7, 9] nothing repeats and the answer is -1. For [6, 6, 6, 6] the answer is 6, since the second 6, at index 1, already repeats.
Approach: remember what you have seen
Read the numbers in order and keep a set of the values met so far. For each number there are two cases. If it is already in the set, we have seen it before, so it is a repeat, and because we read from the left, it is the first one. If it isn't in the set, record it and move on. When the list ends without a repeat, return -1.
A set is the right container because the test num in seen answers right away, without walking through the values. A list would also work, but each check would scan the whole list.
Step by step
- Create an empty set named
seenwithset(). Empty braces{}would make a dictionary. - Loop over
numswithfor num in nums, from left to right. - Check first. If
num in seen, returnnum. This is its second appearance. Nothing repeated earlier, or the function would already have returned. - Then add.
seen.add(num)records the number. The order matters: adding first would find every number in the set, even on its first appearance. - Return
-1after the loop. Reaching the end means no number appeared twice.
Watching the set
For [3, 1, 4, 1, 3], each number is checked against the set before it is added:
num | already in seen? |
|---|---|
| 3 | False |
| 1 | False |
| 4 | False |
| 1 | True |
At the second 1 the answer is returned, and the loop stops. The last 3 is never looked at.
The solution in Python
class Solution:
def firstRepeat(self, nums: List[int]) -> int:
seen = set()
for num in nums:
if num in seen:
return num
seen.add(num)
return -1
Details worth knowing:
returnleaves the loop and the function. That is what makes the answer the first repeat: the function stops at the first number that is already in the set.addchanges the set in place. It returnsNone, and adding a value that is already present does nothing.- Two
returnstatements. One is inside theif, for the case where a repeat is found. The other follows the loop, for the case where none is. Both are needed.
Edge cases and common mistakes
- Adding before checking. If
seen.add(num)came first, every number would be found in the set at once, and the function would return the first element of the list. - Choosing by first appearance. The first value that appears twice is not the first value of the list that has a duplicate. In
[3, 1, 4, 1, 3], 3 showed up first, but 1 repeats first. - Zero is a real value. For
[0, 5, 0]the function returns0. Only-1means "no repeat". - A single number. The loop runs once, nothing is in
seenyet, and the function returns-1.
Testing
[3, 1, 4, 1, 3]gives1.[2, 7, 9]gives-1.[6, 6, 6, 6]gives6.[0, 5, 0]gives0.
