Skip to content

Commit 30789a1

Browse files
chore: add LeetCode daily solution
1 parent 8f4819b commit 30789a1

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# The Two Sneaky Numbers of Digitville (Easy)
2+
3+
**Problem ID:** 3289
4+
**Date:** 2025-10-31
5+
**Link:** https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/
6+
7+
## Approach
8+
9+
To solve the problem of identifying the two sneaky numbers in the list `nums`, we can utilize a straightforward approach based on counting occurrences of each number.
10+
11+
### Approach:
12+
13+
1. **Understanding the Input**: The input list `nums` contains integers from `0` to `n-1`, where each integer is supposed to appear exactly once, except for two integers that appear twice. The length of `nums` is `n + 2`, which indicates that exactly two numbers are duplicated.
14+
15+
2. **Data Structure**: We can use a hash set to track the numbers we encounter as we iterate through the list. This allows us to efficiently check if a number has already been seen.
16+
17+
3. **Iterative Check**: As we traverse the `nums` list:
18+
- For each number, check if it is already in the hash set.
19+
- If it is, it means this number is one of the sneaky numbers, so we add it to our result list.
20+
- If it is not in the set, we add it to the set for future reference.
21+
22+
4. **Stopping Condition**: Since we know there are exactly two duplicates, we can stop once we have found both sneaky numbers.
23+
24+
### Complexity:
25+
- **Time Complexity**: O(n), where n is the length of the input list `nums`, since we are making a single pass through the list.
26+
- **Space Complexity**: O(n) in the worst case for the hash set, although in practice, since we only need to store unique numbers, the space will be limited to the number of unique integers (which is `n`).
27+
28+
### Conclusion:
29+
This approach is efficient and straightforward, leveraging a hash set for quick lookups and insertions to identify the two sneaky numbers in linear time. The algorithm is robust given the constraints and guarantees provided in the problem statement.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.HashMap;
2+
3+
public class Solution {
4+
public int[] findSneakyNumbers(int[] nums) {
5+
HashMap<Integer, Integer> countMap = new HashMap<>();
6+
for (int num : nums) {
7+
countMap.put(num, countMap.getOrDefault(num, 0) + 1);
8+
}
9+
10+
int[] result = new int[2];
11+
int index = 0;
12+
for (int key : countMap.keySet()) {
13+
if (countMap.get(key) == 2) {
14+
result[index++] = key;
15+
}
16+
}
17+
18+
return result;
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var findDuplicates = function(nums) {
2+
const seen = new Set();
3+
const duplicates = [];
4+
5+
for (const num of nums) {
6+
if (seen.has(num)) {
7+
duplicates.push(num);
8+
} else {
9+
seen.add(num);
10+
}
11+
}
12+
13+
return duplicates;
14+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def findDuplicates(self, nums: List[int]) -> List[int]:
3+
seen = set()
4+
duplicates = set()
5+
6+
for num in nums:
7+
if num in seen:
8+
duplicates.add(num)
9+
else:
10+
seen.add(num)
11+
12+
return list(duplicates)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
264264
- 2025-10-28 — [Make Array Elements Equal to Zero](https://leetcode.com/problems/make-array-elements-equal-to-zero/) (Easy) → `Easy/2025-10-28-3354-Make-Array-Elements-Equal-to-Zero`
265265
- 2025-10-29 — [Smallest Number With All Set Bits](https://leetcode.com/problems/smallest-number-with-all-set-bits/) (Easy) → `Easy/2025-10-29-3370-Smallest-Number-With-All-Set-Bits`
266266
- 2025-10-30 — [Minimum Number of Increments on Subarrays to Form a Target Array](https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array/) (Hard) → `Hard/2025-10-30-1526-Minimum-Number-of-Increments-on-Subarrays-to-Form-a-Target-Array`
267+
- 2025-10-31 — [The Two Sneaky Numbers of Digitville](https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/) (Easy) → `Easy/2025-10-31-3289-The-Two-Sneaky-Numbers-of-Digitville`

0 commit comments

Comments
 (0)