Problem overview
The function firstRepeat reads an IntArray, nums, from left to right and returns the first value that appears for the second time, or -1 when no value repeats. For [3, 1, 4, 1, 3] the answer is 1: reading from the left, you meet 3, 1, 4 and then 1 again. The second 1 (index 3) comes before the second 3 (index 4), so 1 wins, even though 3 appeared first. For [2, 7, 9] the answer is -1.
Approach: remember what you have seen
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 number repeated, so it is the answer. If it isn't, add it and move on. Reaching the end of the array means nothing repeated. Checking a set is fast, so one pass is enough.
Step by step
- Create the set
seenwithhashSetOf<Int>(). Thevalmeans the name always refers to this set, which can still grow. - Loop over
numsfrom left to right withfor (num in nums). - Check first. If
seen.contains(num), returnnumat once. - Then add.
seen.add(num)records the number for the next ones. - Return
-1after the loop: it ended without a repeat.
The solution in Kotlin
class Solution {
fun firstRepeat(nums: IntArray): Int {
val seen = hashSetOf<Int>()
for (num in nums) {
if (seen.contains(num)) {
return num
}
seen.add(num)
}
return -1
}
}
Kotlin details worth knowing:
- The order of check and add matters. Adding before checking would find every number in the set, even on its first appearance, and the function would return
nums[0]every time. - Returning from a loop.
returninside theforleaves the whole function, so the loop stops at the first repeat. - A shortcut.
addreturnsfalsewhen the value was already in the set. Soif (!seen.add(num)) return numdoes the check and the add in one step. The lesson's two-step version is easier to read at first. - Why
-1is safe. The values in this lesson are never negative, so-1can't be confused with an answer.
Tracing the example
For [3, 1, 4, 1, 3], the loop stops at the second 1:
3 -> [3]
1 -> [1, 3]
4 -> [1, 3, 4]
1 repeat, seen=[1, 3, 4]
The set prints in hash order, which for small numbers happens to look sorted. The order doesn't matter: only membership does. At the fourth number the check finds 1 already in the set, the function returns 1, and the final 3 is never read.
Complexity
Time: O(n). Each number takes one lookup and at most one insertion, 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
- No repeat.
[2, 7, 9]and a one-element array such as[5]both give-1. - Immediate repeat.
[6, 6, 6, 6]gives6, found at index 1. - Zero is a valid value.
[0, 0]gives0, so a result of 0 is an answer, not a sign of failure. - Choosing the wrong "first". The answer is the value whose second appearance comes first, not the value that appeared first. In
[3, 1, 4, 1, 3]that is 1, not 3.
Testing
[3, 1, 4, 1, 3]gives1.[2, 7, 9]gives-1.[6, 6, 6, 6]gives6.[0, 0]gives0.[5]gives-1.
