Problem overview
The function firstRepeat reads nums from left to right and returns the first value that appears for the second time. If no value appears twice, it returns -1. Every number is between 0 and 10^9, so -1 can't be a real answer.
For [3, 1, 4, 1, 3] the answer is 1. The first 3 comes before the first 1, but the second 1 (index 3) turns up before the second 3 (index 4). The answer is decided by when the repeat is read, not by which value was seen first.
Approach: remember what you have read
Keep a set of the values met so far. For each number, ask whether it is already in the set. If it is, this is its second appearance, and because the numbers are read in order, no earlier repeat exists, so it is the answer. If it isn't, add it and move on. If the loop finishes, nothing repeated.
The alternative is to compare each number with all the ones before it, which takes O(n²) time. The set makes each check a single lookup.
Step by step
- Create the set.
seen := map[int]bool{}starts empty. Go has no set type, so a map from value toboolstands in for one. - Loop over
numswithfor _, num := range nums, in order. - Check first.
if seen[num]is true only for a value stored earlier, so returnnum. A missing key reads asfalse, the zero value ofbool, so no two-value lookup is needed. - Then mark it.
seen[num] = true. The order matters: if you marked the number first, the check would find every number, even on its first appearance. - After the loop, return
-1.
Tracing the example
For [3, 1, 4, 1, 3], the check seen[num] gives false for 3, 1 and 4, and the set grows to three values. At the second 1, seen[1] is true, so the function returns 1 right there. The last 3 is never read.
The solution in Go
func firstRepeat(nums []int) int {
seen := map[int]bool{}
for _, num := range nums {
if seen[num] {
return num
}
seen[num] = true
}
return -1
}
Go details worth knowing
- The zero value of
boolisfalse. Reading a key that isn't in the map givesfalseand doesn't add the key, which is what makesif seen[num]a complete membership test here. - Early return.
return numinside the loop leaves the function immediately, so later numbers are not read. - A
-1that means "none". A sentinel works only when it can't be a real value. Because every number is 0 or more,-1is safe. If negative numbers were allowed, you would return a second value such as(int, bool)instead.
Complexity
The loop reads each number at most once, and each map lookup or insertion takes constant time on average, so the time is O(n). The set holds up to one entry per number read, so the extra space is O(n).
Edge cases and common mistakes
- No repeat.
[2, 7, 9]returns-1. - Immediate repeat.
[6, 6, 6, 6]returns6: the second 6, at index 1, is already a repeat. - Marking before checking returns the first number every time.
- Returning the value seen first rather than the first one to repeat gives 3 instead of 1 for the first example.
- One number. A single element can't repeat, so the answer is
-1.
Testing
[3, 1, 4, 1, 3]returns1.[2, 7, 9]returns-1.[6, 6, 6, 6]returns6.
