Start learning free

LESSON 230 OF 281 · SETS

First Repeat in C++: Detecting Duplicates with unordered_set

Practice Read the full explanation

Given a vector 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 function firstRepeat receives a vector<int> named nums. Reading it from left to right, it returns the first value that appears for the second time, or -1 if no value appears twice. The vector has at least one number, and every number is between 0 and 1,000,000,000, so -1 can never be 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, which is the first repeat. The second 3 comes later, so 3 is not the answer even though it appeared first. For [2, 7, 9] the answer is -1, and for [6, 6, 6, 6] it is 6, because the second 6 is already a repeat.

    Approach: remember what you have seen

    Scan the numbers in order and keep a set of the values met so far. For each number, ask whether it is already in the set. If it is, you have found a value for the second time, and because you read from the left, no earlier number could have been a repeat, or the function would have returned already. If it isn't, add it and move on. When the loop ends without a return, nothing repeated.

    The alternative of comparing every number with every other takes O(n²) time. The set makes the "have I seen this?" question cheap.

    Step by step

    1. Create seen. An empty unordered_set<int>: nothing has been read yet.
    2. Loop over nums in order. A range-based for with an int named num reads from left to right.
    3. Check first. seen.count(num) is 1 if the value is in the set and 0 if not, so seen.count(num) > 0 means this value was met before. Return it.
    4. Then add. seen.insert(num) records the value for later turns.
    5. Return -1 after the loop. No number was found twice.

    The solution in C++

    class Solution {
    public:
        int firstRepeat(vector<int>& nums) {
            unordered_set<int> seen;
            for (int num : nums) {
                if (seen.count(num) > 0) {
                    return num;
                }
                seen.insert(num);
            }
            return -1;
        }
    };
    

    The C++ details worth knowing:

    • The order of the two statements matters. Check, then insert. If you inserted first, every number would be found in the set on its own turn, and the function would return the very first number.
    • count on a set is 0 or 1. A set holds each value at most once, so count works as a membership test. Since C++20 there is also contains, but with C++17 count is the way to ask.
    • insert ignores duplicates. Adding a value already in the set changes nothing. Here that never happens, because a known value has already caused a return.
    • An early return ends the loop. The function stops at the first repeat and does not read the rest of the vector.
    • The sentinel -1 is safe. The task limits the numbers to 0 or more, so -1 can't be confused with a real repeated value.

    Complexity

    Time: O(n) on average. Each number costs one lookup and at most one insertion, both constant time on average. Space: O(n), because in the worst case, when nothing repeats, the set holds every number.

    Edge cases and common mistakes

    • Inserting before checking. The function would return nums[0] every time.
    • Confusing the first value to repeat with the first repeat. In [3, 1, 4, 1, 3], 3 is the first value to appear, but 1 is the first to appear again, and the answer is 1.
    • Several repeats. Only the first one found matters; the function returns on it.
    • All the same. [6, 6, 6, 6] returns 6 at index 1.
    • A single number. [7] has nothing to repeat, so the answer is -1.

    Testing

    • [3, 1, 4, 1, 3] gives 1.
    • [2, 7, 9] gives -1.
    • [6, 6, 6, 6] gives 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