Start learning free

LESSON 235 OF 285 · SETS

First Repeat in Go: Finding a Duplicate with a Set

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

    GO · SETS

    First Repeat in Go, explained

    Problem overview

    The function firstRepeat reads nums from left to right and returns the first value that appears for the second time. If no value appears twice, it returns -1. Every number is between 0 and 10^9, so -1 can't be a real answer.

    For [3, 1, 4, 1, 3] the answer is 1. The first 3 comes before the first 1, but the second 1 (index 3) turns up before the second 3 (index 4). The answer is decided by when the repeat is read, not by which value was seen first.

    Approach: remember what you have read

    Keep a set of the values met so far. For each number, ask whether it is already in the set. If it is, this is its second appearance, and because the numbers are read in order, no earlier repeat exists, so it is the answer. If it isn't, add it and move on. If the loop finishes, nothing repeated.

    The alternative is to compare each number with all the ones before it, which takes O(n²) time. The set makes each check a single lookup.

    Step by step

    1. Create the set. seen := map[int]bool{} starts empty. Go has no set type, so a map from value to bool stands in for one.
    2. Loop over nums with for _, num := range nums, in order.
    3. Check first. if seen[num] is true only for a value stored earlier, so return num. A missing key reads as false, the zero value of bool, so no two-value lookup is needed.
    4. Then mark it. seen[num] = true. The order matters: if you marked the number first, the check would find every number, even on its first appearance.
    5. After the loop, return -1.

    Tracing the example

    For [3, 1, 4, 1, 3], the check seen[num] gives false for 3, 1 and 4, and the set grows to three values. At the second 1, seen[1] is true, so the function returns 1 right there. The last 3 is never read.

    The solution in Go

    func firstRepeat(nums []int) int {
        seen := map[int]bool{}
        for _, num := range nums {
            if seen[num] {
                return num
            }
            seen[num] = true
        }
        return -1
    }
    

    Go details worth knowing

    • The zero value of bool is false. Reading a key that isn't in the map gives false and doesn't add the key, which is what makes if seen[num] a complete membership test here.
    • Early return. return num inside the loop leaves the function immediately, so later numbers are not read.
    • A -1 that means "none". A sentinel works only when it can't be a real value. Because every number is 0 or more, -1 is safe. If negative numbers were allowed, you would return a second value such as (int, bool) instead.

    Complexity

    The loop reads each number at most once, and each map lookup or insertion takes constant time on average, so the time is O(n). The set holds up to one entry per number read, so the extra space is O(n).

    Edge cases and common mistakes

    • No repeat. [2, 7, 9] returns -1.
    • Immediate repeat. [6, 6, 6, 6] returns 6: the second 6, at index 1, is already a repeat.
    • Marking before checking returns the first number every time.
    • Returning the value seen first rather than the first one to repeat gives 3 instead of 1 for the first example.
    • One number. A single element can't repeat, so the answer is -1.

    Testing

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