Start learning free

LESSON 236 OF 292 · SETS

First Repeat in Swift: Detecting Duplicates 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

    SWIFT · SETS

    First Repeat in Swift, explained

    Problem overview

    The function firstRepeat receives an array of integers, nums. Reading from left to right, it returns the first value that appears for the second time. If no value appears twice, it returns -1.

    The answer depends on where the second occurrence is, not the first. For [3, 1, 4, 1, 3], the second 1 (index 3) comes before the second 3 (index 4), so the answer is 1, although 3 appeared first. For [2, 7, 9] nothing repeats, so the result is -1, and 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

    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 time, and because the loop reads from the left, no earlier number could have repeated first, so return it. If it isn't, add it and move on.

    The check on a set is fast, so the whole scan takes one pass. The slower alternative compares each number with all the ones before it.

    Step by step

    1. Create an empty Set<Int> named seen, with var, because values are added to it.
    2. Loop over nums with for num in nums.
    3. Check first: if seen.contains(num), return num.
    4. Then add the number with seen.insert(num).
    5. Return -1 after the loop, which means the loop never found a repeat.

    The solution in Swift

    class Solution {
        func firstRepeat(_ nums: [Int]) -> Int {
            var seen = Set<Int>()
            for num in nums {
                if seen.contains(num) {
                    return num
                }
                seen.insert(num)
            }
            return -1
        }
    }
    

    Swift details

    • Check, then insert. If the number were inserted first, every number would be found in seen, even on its first appearance, and the function would return the first element every time.
    • insert returns a tuple. seen.insert(num) returns (inserted: Bool, memberAfterInsert: Int), and inserted is false when the value was already present. The lesson ignores that result and uses contains. Swift marks insert as discardable, so ignoring the result gives no warning. A common shortcut is if !seen.insert(num).inserted { return num }, which checks and adds in one call.
    • The empty set. Set<Int>() names the element type, because there are no values yet to infer it from.
    • Early return. A return inside the loop ends the whole function, so the rest of the array is never read.

    Complexity

    Time: O(n) on average. The loop runs at most n times, and contains and insert take constant time on average.

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

    Edge cases and common mistakes

    • No repeat. [2, 7, 9] returns -1. The lesson's numbers are never negative, so -1 can't be confused with a real answer.
    • The repeat that comes first. [3, 1, 4, 1, 3] returns 1, not 3: judge by the second occurrence.
    • Immediate repeat. [6, 6, 6, 6] returns 6 at index 1.
    • A single number. [5] returns -1.
    • Inserting before checking. It makes the first number come back as a repeat.

    Testing

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