Start learning free

LESSON 235 OF 291 · SETS

First Repeat in Scala: 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

    SCALA · SETS

    First Repeat in Scala, explained

    Problem overview

    The method firstRepeat receives an Array[Int] named nums. It reads the array from left to right and returns the first value that appears for the second time, or -1 if no value appears twice. The numbers in the lesson are never negative, so -1 can't be mistaken for a real answer.

    For Array(3, 1, 4, 1, 3) the answer is 1. Reading left to right, the second 1 (index 3) shows up before the second 3 (index 4), even though 3 appeared first overall. For Array(2, 7, 9) the answer is -1, and for Array(6, 6, 6, 6) it is 6, because the second 6 at index 1 is already a repeat.

    Approach

    Keep a set of the values met so far. For each number, ask whether it is already in the set. If yes, it is the first number to be seen a second time, so return it immediately. If no, add it and carry on. A set answers "have I seen this?" in constant time on average, so the whole scan is one pass. The brute force compares each number with all the earlier ones, which takes O(n²) time.

    Step by step

    1. Create the set. scala.collection.mutable.Set[Int]() in a val named seen. The val keeps the name on one set, which can still grow.
    2. Loop left to right with for (num <- nums).
    3. Check first. If seen.contains(num), return num.
    4. Then add. seen += num puts the number in the set.
    5. If the loop ends, return -1: nothing repeated.

    The solution in Scala

    class Solution {
        def firstRepeat(nums: Array[Int]): Int = {
            val seen = scala.collection.mutable.Set[Int]()
            for (num <- nums) {
                if (seen.contains(num)) {
                    return num
                }
                seen += num
            }
            return -1
        }
    }
    
    • Check, then add. If the number were added first, the check would find every number in the set, even on its first appearance.
    • return inside a loop. The method leaves at the first repeat, and the rest of the array is never read. In Scala 3.5 the compiler prints a warning that non-local returns are no longer supported and suggests boundary from scala.util. The code still compiles and returns the right values for the lesson's examples.
    • += on a set. On a mutable set, seen += num adds the value in place. Adding a value that's present changes nothing.
    • Int result. The signature returns an Int, and -1 is an ordinary Int that the lesson's constraints keep out of the input.

    Following the loop

    For Array(3, 1, 4, 1, 3), here is what the set holds when each number is checked:

    numseen before the checkalready in it?
    3HashSet()no
    1HashSet(3)no
    4HashSet(1, 3)no
    1HashSet(1, 3, 4)yes, return 1

    The loop stops at the fourth number, so the final 3 is never examined.

    Complexity

    Time: O(n) on average. Each number costs one membership check and at most one insertion, 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

    • No repeat. Array(2, 7, 9) reaches the end and returns -1.
    • Immediate repeat. Array(6, 6, 6, 6) returns 6 at index 1.
    • Not the first number to appear twice. In Array(3, 1, 4, 1, 3), the answer is 1, not 3. The rule is about the order of the second appearances.
    • Returning inside the wrong place. A return -1 inside the loop would stop after the first number.

    Testing

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