|
| 1 | +--- |
| 2 | +description: "Author: @wingkwong | https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/" |
| 3 | +tags: [Array, Hash Table, Math] |
| 4 | +--- |
| 5 | + |
| 6 | +# 3289 - The Two Sneaky Numbers of Digitville (Easy) |
| 7 | + |
| 8 | +## Problem Link |
| 9 | + |
| 10 | +https://leetcode.com/problems/the-two-sneaky-numbers-of-digitville/ |
| 11 | + |
| 12 | +## Problem Statement |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +**Input:** nums = [0,1,1,0] |
| 21 | + |
| 22 | +**Output:** [0,1] |
| 23 | + |
| 24 | +**Explanation:** |
| 25 | + |
| 26 | +The numbers 0 and 1 each appear twice in the array. |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | + |
| 30 | +**Input:** nums = [0,3,2,1,3,2] |
| 31 | + |
| 32 | +**Output:** [2,3] |
| 33 | + |
| 34 | +**Explanation:** |
| 35 | + |
| 36 | +The numbers 2 and 3 each appear twice in the array. |
| 37 | + |
| 38 | +**Example 3:** |
| 39 | + |
| 40 | +**Input:** nums = [7,1,5,4,3,4,6,0,9,5,8,2] |
| 41 | + |
| 42 | +**Output:** [4,5] |
| 43 | + |
| 44 | +**Explanation:** |
| 45 | + |
| 46 | +The numbers 4 and 5 each appear twice in the array. |
| 47 | + |
| 48 | +**Constraints:** |
| 49 | + |
| 50 | +- `2 <= n <= 100` |
| 51 | +- `nums.length == n + 2` |
| 52 | +- `0 <= nums[i] < n` |
| 53 | +- The input is generated such that `nums` contains **exactly** two repeated elements. |
| 54 | + |
| 55 | +## Approach 1: Counter |
| 56 | + |
| 57 | +We can simply count the frequency and find which two numbers have appear more than one time. |
| 58 | + |
| 59 | +<Tabs> |
| 60 | +<TabItem value="py" label="Python"> |
| 61 | +<SolutionAuthor name="@wingkwong"/> |
| 62 | + |
| 63 | +```py |
| 64 | +class Solution: |
| 65 | + def getSneakyNumbers(self, nums: List[int]) -> List[int]: |
| 66 | + cnt = Counter(nums) |
| 67 | + return [x for x, f in cnt.items() if f > 1] |
| 68 | +``` |
| 69 | + |
| 70 | +</TabItem> |
| 71 | +</Tabs> |
0 commit comments