Loading

LeetCode problem#3289

The Two Sneaky Numbers of Digitville

Solved in 1 languageJava

Solve it on LeetCode(opens in a new tab)

Solution

21 lines
1class Solution {
2    public int[] getSneakyNumbers(int[] nums) {
3        HashSet<Integer> hashSet = new HashSet<Integer>();
4        int[] numbers = new int[2];
5        int numberIndex = 0;
6
7        for (int i = 0; numberIndex < 2 && i < nums.length; i++) {
8            int num = nums[i];
9
10            if (hashSet.contains(num)) {
11                numbers[numberIndex] = num;
12                numberIndex++;
13            }
14            else {
15                hashSet.add(num);
16            }
17        }
18
19        return numbers;
20    }
21}

Want a walkthrough of this Java solution?

Get a step-by-step explanation of the idea behind the code.

All problems

Problem statements belong to LeetCode and are on leetcode.com. We're not affiliated with LeetCode.