Leetcode / 3289. The Two Sneaky Numbers of Digitville
Pick a programming language:
Here is the source code for the solution to this problem.
class Solution {
public int[] getSneakyNumbers(int[] nums) {
HashSet<Integer> hashSet = new HashSet<Integer>();
int[] numbers = new int[2];
int numberIndex = 0;
for (int i = 0; numberIndex < 2 && i < nums.length; i++) {
int num = nums[i];
if (hashSet.contains(num)) {
numbers[numberIndex] = num;
numberIndex++;
}
else {
hashSet.add(num);
}
}
return numbers;
}
}
Did you like the lesson? 😆👍
Consider a donation to support our work: