Problem overview
The method firstRepeat receives an Array[Int] named nums. It reads the array from left to right and returns the first value that appears for the second time, or -1 if no value appears twice. The numbers in the lesson are never negative, so -1 can't be mistaken for a real answer.
For Array(3, 1, 4, 1, 3) the answer is 1. Reading left to right, the second 1 (index 3) shows up before the second 3 (index 4), even though 3 appeared first overall. For Array(2, 7, 9) the answer is -1, and for Array(6, 6, 6, 6) it is 6, because the second 6 at index 1 is already a repeat.
Approach
Keep a set of the values met so far. For each number, ask whether it is already in the set. If yes, it is the first number to be seen a second time, so return it immediately. If no, add it and carry on. A set answers "have I seen this?" in constant time on average, so the whole scan is one pass. The brute force compares each number with all the earlier ones, which takes O(n²) time.
Step by step
- Create the set.
scala.collection.mutable.Set[Int]()in avalnamedseen. Thevalkeeps the name on one set, which can still grow. - Loop left to right with
for (num <- nums). - Check first. If
seen.contains(num), returnnum. - Then add.
seen += numputs the number in the set. - If the loop ends, return -1: nothing repeated.
The solution in Scala
class Solution {
def firstRepeat(nums: Array[Int]): Int = {
val seen = scala.collection.mutable.Set[Int]()
for (num <- nums) {
if (seen.contains(num)) {
return num
}
seen += num
}
return -1
}
}
- Check, then add. If the number were added first, the check would find every number in the set, even on its first appearance.
returninside a loop. The method leaves at the first repeat, and the rest of the array is never read. In Scala 3.5 the compiler prints a warning that non-local returns are no longer supported and suggestsboundaryfromscala.util. The code still compiles and returns the right values for the lesson's examples.+=on a set. On a mutable set,seen += numadds the value in place. Adding a value that's present changes nothing.Intresult. The signature returns anInt, and -1 is an ordinaryIntthat the lesson's constraints keep out of the input.
Following the loop
For Array(3, 1, 4, 1, 3), here is what the set holds when each number is checked:
| num | seen before the check | already in it? |
|---|---|---|
| 3 | HashSet() | no |
| 1 | HashSet(3) | no |
| 4 | HashSet(1, 3) | no |
| 1 | HashSet(1, 3, 4) | yes, return 1 |
The loop stops at the fourth number, so the final 3 is never examined.
Complexity
Time: O(n) on average. Each number costs one membership check 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.
Array(2, 7, 9)reaches the end and returns -1. - Immediate repeat.
Array(6, 6, 6, 6)returns 6 at index 1. - Not the first number to appear twice. In
Array(3, 1, 4, 1, 3), the answer is 1, not 3. The rule is about the order of the second appearances. - Returning inside the wrong place. A
return -1inside the loop would stop after the first number.
Testing
Array(3, 1, 4, 1, 3)gives1.Array(2, 7, 9)gives-1.Array(6, 6, 6, 6)gives6.
