Start learning free

LESSON 235 OF 289 · SETS

First Repeat in TypeScript: Finding the First 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

    TYPESCRIPT · SETS

    First Repeat in TypeScript, explained

    Problem overview

    The method firstRepeat receives an array 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 appears twice. The numbers are between 0 and 1,000,000,000, so -1 can never be an element of the input.

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

    Approach

    Remember the values already read in a set. Each new number then needs only one question: is it in the set?

    • If yes, this is its second appearance and no earlier number repeated, so return it.
    • If no, add it to the set and move on.

    Reaching the end of the loop means no value was ever seen twice, so the method returns -1.

    Checking membership in a set is fast, so the method makes a single pass. The slow alternative compares each number with all the ones before it, which takes O(n²) time.

    Step by step

    1. Create the empty set. seen holds the values met so far. It starts empty because nothing has been read yet.
    2. Read left to right. A for...of loop over nums visits the numbers in order, which is what "first" depends on.
    3. Check first. If seen.has(num) is true, return num.
    4. Then add. seen.add(num) records the number, still inside the loop. Adding before checking would find every number already in the set, even on its first appearance.
    5. Return -1 after the loop.

    The solution in TypeScript

    class Solution {
        firstRepeat(nums: number[]): number {
            let seen: Set<number> = new Set();
            for (let num of nums) {
                if (seen.has(num)) {
                    return num;
                }
                seen.add(num);
            }
            return -1;
        }
    }
    

    Details worth knowing:

    • The type of the variable. A set of numbers is written Set<number>. The annotation is erased when TypeScript compiles to JavaScript. In code of your own you can also write const seen = new Set<number>();.
    • has and add. seen.has(num) returns true or false. seen.add(num) inserts the value, and does nothing if it is already there.
    • Returning inside the loop. The first repeat found ends the method at once, so the rest of the array is never read.
    • Why -1 is safe. The numbers are never negative, so the answer -1 can't be confused with a real repeated value. A value of 0 is a valid answer: [0, 5, 0] returns 0.

    Complexity

    Time: O(n). Each number takes one has and at most one add, both constant time on average.

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

    Edge cases and common mistakes

    • Adding before checking. The method would return the first number of the array every time.
    • Returning the value that appeared first. In [3, 1, 4, 1, 3] the answer is 1, the value whose second appearance comes first, not 3.
    • Treating 0 as "not found". Only -1 means that nothing repeats. A returned 0 is a real value.
    • One number. [7] has no repeat, so the loop ends and -1 is returned.
    • Forgetting the final return -1. The method would return undefined when nothing repeats.

    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 TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free