Start learning free

LESSON 234 OF 289 · SETS

First Repeat in C#: Finding a Duplicate with HashSet

Practice Read the full explanation

Given an array of integers nums, read it from left to right and return the first value that appears for the second time.

If no value appears twice, return -1.

 

Example 1:

Input: nums = [3, 1, 4, 1, 3]
Output: 1
Explanation: Reading from the left: 3, 1, 4, then 1 again. The second 1 (index 3) comes before the second 3 (index 4), so the answer is 1, although 3 showed up first.

Example 2:

Input: nums = [2, 7, 9]
Output: -1
Explanation: Every value appears once, so nothing repeats.

Example 3:

Input: nums = [6, 6, 6, 6]
Output: 6
Explanation: The second 6, at index 1, is already a repeat.

 

Constraints:

  • nums has at least one number.
  • Every number is between 0 and 109, so -1 always means that no value repeats.
STEP 1 OF 5
    Ctrl + Enter to check

    C# · SETS

    First Repeat in C#, explained

    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

    1. Create an empty HashSet<int> named seen. Nothing has been read yet.
    2. Write a foreach loop over nums with an int named num. It reads the numbers in order from the left.
    3. Check the set first. If seen.Contains(num) is true, return num: this is its second time.
    4. 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.
    5. 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 return inside the loop ends the method at once, so the rest of the array isn't read.
    • Contains and Add are two separate calls here. Add also returns false when 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

    numsResult
    [3, 1, 4, 1, 3]1
    [2, 7, 9]-1
    [6, 6, 6, 6]6
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free