Start learning free

LESSON 216 OF 501 · SETS

First Repeat in JavaScript

Practice

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

    First Repeat in JavaScript

    Keep going with JavaScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free