Skip to content

3289. The Two Sneaky Numbers of Digitville 👍

  • Time: $O(n)$
  • Space: $O(100) = O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
  public int[] getSneakyNumbers(int[] nums) {
    final int MAX = 100;
    int[] ans = new int[2];
    int[] count = new int[MAX + 1];
    int ansIndex = 0;

    for (final int num : nums)
      if (++count[num] == 2)
        ans[ansIndex++] = num;

    return ans;
  }
}
1
2
3
4
class Solution:
  def getSneakyNumbers(self, nums: list[int]) -> list[int]:
    return [num for num, freq in collections.Counter(nums).items()
            if freq == 2]