The Two Sneaky Numbers of Digitville
EASYDescription
In the town of Digitville, there was a list of numbers called nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual.
As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.
Example 1:
Input: nums = [0,1,1,0]
Output: [0,1]
Explanation:
The numbers 0 and 1 each appear twice in the array.
Example 2:
Input: nums = [0,3,2,1,3,2]
Output: [2,3]
Explanation:
The numbers 2 and 3 each appear twice in the array.
Example 3:
Input: nums = [7,1,5,4,3,4,6,0,9,5,8,2]
Output: [4,5]
Explanation:
The numbers 4 and 5 each appear twice in the array.
Constraints:
2 <= n <= 100nums.length == n + 20 <= nums[i] < n- The input is generated such that
numscontains exactly two repeated elements.
Approaches
Checkout 4 different approaches to solve The Two Sneaky Numbers of Digitville. Click on different approaches to view the approach and algorithm in detail.
Brute Force with Nested Loops
The most straightforward, yet least efficient, way to solve this problem is by using a brute-force approach. We can compare every number in the array with every other number to find pairs that are identical. This involves using two nested loops.
Algorithm
- Initialize an empty
Setcalledduplicatesto store the results and avoid adding the same number twice. - Use a nested loop structure. The outer loop iterates from
i = 0tonums.length - 1. - The inner loop iterates from
j = i + 1tonums.length - 1. - Inside the inner loop, compare
nums[i]andnums[j]. - If
nums[i] == nums[j], it signifies a duplicate. Addnums[i]to theduplicatesset. - After the loops complete, convert the
duplicatesset into an array and return it.
In this method, we take each element of the array and scan the rest of the array to see if a duplicate of that element exists. We use a Set to store the found duplicates, which conveniently handles the issue of finding the same duplicate pair multiple times. For example, in [1, 1, 1], comparing the first and second 1s finds a duplicate, and comparing the first and third 1s finds the same duplicate. The set ensures we only store 1 once.
import java.util.HashSet;
import java.util.Set;
class Solution {
public int[] findTwoSneakyNumbers(int[] nums) {
Set<Integer> duplicates = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] == nums[j]) {
duplicates.add(nums[i]);
}
}
}
int[] result = new int[duplicates.size()];
int index = 0;
for (int num : duplicates) {
result[index++] = num;
}
return result;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- Uses constant extra space (as the result set size is fixed at 2).
- Extremely inefficient for larger arrays due to its quadratic time complexity.
- Likely to result in a 'Time Limit Exceeded' error on most coding platforms for non-trivial input sizes.
Code Solutions
Checking out 3 solutions in different languages for The Two Sneaky Numbers of Digitville. Click on different languages to view the code.
class Solution {
public
int[] getSneakyNumbers(int[] nums) {
int[] ans = new int[2];
int[] cnt = new int[100];
int k = 0;
for (int x : nums) {
if (++cnt[x] == 2) {
ans[k++] = x;
}
}
return ans;
}
}
Video Solution
Watch the video walkthrough for The Two Sneaky Numbers of Digitville
Similar Questions
5 related questions you might find useful
Patterns:
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.