Start learning free

LESSON 235 OF 291 · SETS

First Repeat in Kotlin: Spotting Duplicates with a HashSet

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

    KOTLIN · SETS

    First Repeat in Kotlin, explained

    Problem overview

    The function firstRepeat reads an IntArray, nums, from left to right and returns the first value that appears for the second time, or -1 when no value repeats. For [3, 1, 4, 1, 3] the answer is 1: reading from the left, you meet 3, 1, 4 and then 1 again. The second 1 (index 3) comes before the second 3 (index 4), so 1 wins, even though 3 appeared first. For [2, 7, 9] the answer is -1.

    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 appearance, and because the numbers are read in order, no earlier number repeated, so it is the answer. If it isn't, add it and move on. Reaching the end of the array means nothing repeated. Checking a set is fast, so one pass is enough.

    Step by step

    1. Create the set seen with hashSetOf<Int>(). The val means the name always refers to this set, which can still grow.
    2. Loop over nums from left to right with for (num in nums).
    3. Check first. If seen.contains(num), return num at once.
    4. Then add. seen.add(num) records the number for the next ones.
    5. Return -1 after the loop: it ended without a repeat.

    The solution in Kotlin

    class Solution {
        fun firstRepeat(nums: IntArray): Int {
            val seen = hashSetOf<Int>()
            for (num in nums) {
                if (seen.contains(num)) {
                    return num
                }
                seen.add(num)
            }
            return -1
        }
    }
    

    Kotlin details worth knowing:

    • The order of check and add matters. Adding before checking would find every number in the set, even on its first appearance, and the function would return nums[0] every time.
    • Returning from a loop. return inside the for leaves the whole function, so the loop stops at the first repeat.
    • A shortcut. add returns false when the value was already in the set. So if (!seen.add(num)) return num does the check and the add in one step. The lesson's two-step version is easier to read at first.
    • Why -1 is safe. The values in this lesson are never negative, so -1 can't be confused with an answer.

    Tracing the example

    For [3, 1, 4, 1, 3], the loop stops at the second 1:

    3 -> [3]
    1 -> [1, 3]
    4 -> [1, 3, 4]
    1 repeat, seen=[1, 3, 4]

    The set prints in hash order, which for small numbers happens to look sorted. The order doesn't matter: only membership does. At the fourth number the check finds 1 already in the set, the function returns 1, and the final 3 is never read.

    Complexity

    Time: O(n). Each number takes one lookup 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. [2, 7, 9] and a one-element array such as [5] both give -1.
    • Immediate repeat. [6, 6, 6, 6] gives 6, found at index 1.
    • Zero is a valid value. [0, 0] gives 0, so a result of 0 is an answer, not a sign of failure.
    • Choosing the wrong "first". The answer is the value whose second appearance comes first, not the value that appeared first. In [3, 1, 4, 1, 3] that is 1, not 3.

    Testing

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