Problem overview
The method FirstRepeat receives an int[] named nums, reads it from left to right and returns the first value that appears for the second time. If no value appears twice, it returns -1. The numbers are between 0 and 1,000,000,000, so -1 can't be confused with a real answer.
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 is the answer, 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 what you have seen
The question "have I met this number before?" is exactly what a set answers. Keep a HashSet<int> of the numbers read so far. For each new number, ask the set. If the number is in it, this is its second appearance, and it is the answer. If not, add it and continue.
Checking a HashSet is fast, on average a constant-time step, so one pass over the array is enough. Comparing every number with every earlier one would take O(n²) time, and the set avoids that.
Reading in order is what makes the answer "the first value to repeat". The loop stops at the first number that is already in the set, so no repeat can have come earlier. On [3, 1, 4, 1, 3] the set grows to 3, 1, 4, then the next 1 is found in it and the method returns 1 without ever reaching the last 3.
Step by step
- Create an empty
HashSet<int>namedseen. Nothing has been read yet. - Write a
foreachloop overnumswith anintnamednum. It reads the numbers in order from the left. - Check the set first. If
seen.Contains(num)istrue, returnnum: this is its second time. - Then add it with
seen.Add(num). If you added first, every number would already be in the set when you checked, and the first number would be reported as a repeat. - Return -1 after the loop. Reaching the end means no value appeared twice.
The solution in C#
class Solution {
public int FirstRepeat(int[] nums) {
HashSet<int> seen = new HashSet<int>();
foreach (int num in nums) {
if (seen.Contains(num)) {
return num;
}
seen.Add(num);
}
return -1;
}
}
Details worth knowing:
- The
returninside the loop ends the method at once, so the rest of the array isn't read. ContainsandAddare two separate calls here.Addalso returnsfalsewhen the value was already present, which would let you combine them, but the lesson keeps them apart so each line does one thing.
Complexity
The loop runs at most n times, and Contains and Add take constant time on average, so the time is O(n). In the worst case, when nothing repeats, the set holds all n numbers, so the space is O(n).
Edge cases and common mistakes
- Adding before checking. Every number would look like a repeat.
- Returning the first value that has a duplicate. In
[3, 1, 4, 1, 3]that would be 3, but the answer is 1, because its second copy comes first. - No repeat. The method returns -1, which is safe because every number is 0 or more.
- Adjacent repeats. In
[6, 6, 6, 6]the method returns at index 1.
Testing
| nums | Result |
|---|---|
[3, 1, 4, 1, 3] | 1 |
[2, 7, 9] | -1 |
[6, 6, 6, 6] | 6 |
