3289. The Two Sneaky Numbers of Digitville LeetCode Solution
In this guide, you will get 3289. The Two Sneaky Numbers of Digitville LeetCode Solution with the best time and space complexity. The solution to The Two Sneaky Numbers of Digitville problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- The Two Sneaky Numbers of Digitville solution in C++
- The Two Sneaky Numbers of Digitville solution in Java
- The Two Sneaky Numbers of Digitville solution in Python
- Additional Resources
Problem Statement of The Two Sneaky Numbers of Digitville
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 <= 100
nums.length == n + 2
0 <= nums[i] < n
The input is generated such that nums contains exactly two repeated elements.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(100) = O(1)
3289. The Two Sneaky Numbers of Digitville LeetCode Solution in C++
class Solution {
public:
vector<int> getSneakyNumbers(const vector<int>& nums) {
constexpr int kMax = 100;
vector<int> ans;
vector<int> count(kMax + 1);
for (const int num : nums)
if (++count[num] == 2)
ans.push_back(num);
return ans;
}
};
/* code provided by PROGIEZ */
3289. The Two Sneaky Numbers of Digitville LeetCode Solution in Java
class Solution {
public int[] getSneakyNumbers(int[] nums) {
final int kMax = 100;
int[] ans = new int[2];
int[] count = new int[kMax + 1];
int ansIndex = 0;
for (final int num : nums)
if (++count[num] == 2)
ans[ansIndex++] = num;
return ans;
}
}
// code provided by PROGIEZ
3289. The Two Sneaky Numbers of Digitville LeetCode Solution in Python
class Solution:
def getSneakyNumbers(self, nums: list[int]) -> list[int]:
return [num for num, freq in collections.Counter(nums).items()
if freq == 2]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.