Start learning free

LESSON 238 OF 293 · SETS

First Repeat in Rust: Finding a Duplicate with a 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

    RUST · SETS

    First Repeat in Rust, explained

    Problem overview

    The function first_repeat receives a vector of integers, nums. Reading it from left to right, it returns the first value that appears for the second time, or -1 if no value repeats. All numbers are between 0 and 10^9, so -1 can't be confused with a real answer.

    For [3, 1, 4, 1, 3] the answer is 1. The reading goes 3, 1, 4, and then 1 again at index 3. The second 3 comes later, at index 4, so 3 loses even though it appeared first. For [2, 7, 9] nothing repeats and the answer is -1.

    Approach: remember what you have seen

    Comparing every pair of numbers would work but costs O(n²). A set of the values met so far makes each check fast. For every number, ask the set: have I seen this before? If yes, this is the second appearance, and it is the first one to happen in reading order, so return it. If no, add it to the set and move on.

    The order of the two actions matters. Check first, then add. If the number were added before the check, it would always be found.

    Step by step

    1. Create the empty set seen, a mutable HashSet<i32>.
    2. Loop over &nums with the variable num, reading from left to right.
    3. Check the set. If seen.contains(num), return *num, the value behind the reference.
    4. Add the number with seen.insert(*num), after the check and still inside the loop.
    5. Return -1 after the loop: it ended without returning, so nothing repeated.

    The solution in Rust

    impl Solution {
        fn first_repeat(nums: Vec<i32>) -> i32 {
            let mut seen: HashSet<i32> = HashSet::new();
            for num in &nums {
                if seen.contains(num) {
                    return *num;
                }
                seen.insert(*num);
            }
            return -1;
        }
    }
    

    The Rust details worth a closer look:

    • References. Looping over &nums makes num a &i32. contains takes a reference, so num is passed as is. The function returns an i32 and the set stores i32 values, so both places use *num to read the integer behind the reference.
    • Early return. A return inside the loop leaves the whole function at once, so the loop never reads the numbers after the first repeat.
    • insert tells you too. insert returns true when the value was new and false when it was already present. The lesson writes the check and the insert as two steps to keep them clear.
    • Why -1 is safe. The values are never negative, so the sentinel return value can't be a real number from the vector.

    Complexity

    Time: O(n) on average. Each number causes one lookup and at most one insertion, each constant time on average.

    Space: O(n). In the worst case, when nothing repeats, the set holds every number.

    Tracing the example

    With [3, 1, 4, 1, 3], the set starts empty. 3 is not in it, so it is added. The same happens for 1 and 4. The next number is 1, and the set already holds it, so the function returns 1 at once. The final 3 is never read.

    Edge cases and common mistakes

    • Inserting before checking. Every number would already be in the set when checked, and the function would return the first element for any input.
    • Returning the first value that has a duplicate. That is a different question. In [3, 1, 4, 1, 3] it would give 3, but the answer here is 1, the value whose second appearance comes first.
    • Immediate repeat. [6, 6, 6, 6] returns 6 at index 1.
    • No repeat. The loop finishes and the function returns -1.

    Testing

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