Start learning free

LESSON 231 OF 287 · SETS

First Repeat in Python: Finding a Duplicate with a Set

Practice Read the full explanation

Given a list 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

    PYTHON · SETS

    First Repeat in Python, explained

    Problem overview

    The method firstRepeat receives a list of integers, nums. Reading the list from left to right, it returns the first value that appears for the second time, or -1 if no value appears twice. The numbers are never negative, so -1 can't be mistaken for a real answer.

    For [3, 1, 4, 1, 3] the answer is 1. Reading from the left gives 3, 1, 4, and then 1 again, so the second 1, at index 3, is the first repeat. The second 3, at index 4, comes later, even though 3 showed up first. For [2, 7, 9] nothing repeats and the answer is -1. For [6, 6, 6, 6] the answer is 6, since the second 6, at index 1, already repeats.

    Approach: remember what you have seen

    Read the numbers in order and keep a set of the values met so far. For each number there are two cases. If it is already in the set, we have seen it before, so it is a repeat, and because we read from the left, it is the first one. If it isn't in the set, record it and move on. When the list ends without a repeat, return -1.

    A set is the right container because the test num in seen answers right away, without walking through the values. A list would also work, but each check would scan the whole list.

    Step by step

    1. Create an empty set named seen with set(). Empty braces {} would make a dictionary.
    2. Loop over nums with for num in nums, from left to right.
    3. Check first. If num in seen, return num. This is its second appearance. Nothing repeated earlier, or the function would already have returned.
    4. Then add. seen.add(num) records the number. The order matters: adding first would find every number in the set, even on its first appearance.
    5. Return -1 after the loop. Reaching the end means no number appeared twice.

    Watching the set

    For [3, 1, 4, 1, 3], each number is checked against the set before it is added:

    numalready in seen?
    3False
    1False
    4False
    1True

    At the second 1 the answer is returned, and the loop stops. The last 3 is never looked at.

    The solution in Python

    class Solution:
        def firstRepeat(self, nums: List[int]) -> int:
            seen = set()
            for num in nums:
                if num in seen:
                    return num
                seen.add(num)
            return -1
    

    Details worth knowing:

    • return leaves the loop and the function. That is what makes the answer the first repeat: the function stops at the first number that is already in the set.
    • add changes the set in place. It returns None, and adding a value that is already present does nothing.
    • Two return statements. One is inside the if, for the case where a repeat is found. The other follows the loop, for the case where none is. Both are needed.

    Edge cases and common mistakes

    • Adding before checking. If seen.add(num) came first, every number would be found in the set at once, and the function would return the first element of the list.
    • Choosing by first appearance. The first value that appears twice is not the first value of the list that has a duplicate. In [3, 1, 4, 1, 3], 3 showed up first, but 1 repeats first.
    • Zero is a real value. For [0, 5, 0] the function returns 0. Only -1 means "no repeat".
    • A single number. The loop runs once, nothing is in seen yet, and the function returns -1.

    Testing

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