Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created October 31, 2025 17:23
Show Gist options
  • Select an option

  • Save Ifihan/b16b6f33af7e01744e72144472c0266e to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/b16b6f33af7e01744e72144472c0266e to your computer and use it in GitHub Desktop.
The Two Sneaky Numbers of Digitville

Question

Approach

I scan the array once while keeping a set of numbers I have already seen. Whenever I encounter a number that is already in the set, it must be one of the sneaky repeated numbers, so I add it to the result list. Since there are exactly two repeats, I stop once I found both.

Implementation

class Solution:
    def getSneakyNumbers(self, nums: List[int]) -> List[int]:
        seen = set()
        res = []
        for x in nums:
            if x in seen:
                res.append(x)
                if len(res) == 2:
                    break
            else:
                seen.add(x)
        return res

Complexities

  • Time: O(n)
  • Space: O(n)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment