Skip to main content
Back to problems
#3289
Easy Algorithms

The two sneaky numbers of digitville

Array Hash Table Math
89.9% acceptance
Feb 25, 2026
521
22
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. Return an array of size two containing the two numbers (in any order).

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn get_sneaky_numbers(nums: Vec<i32>) -> Vec<i32> {
    let mut count = vec![0u8; nums.len()];
    let mut res = Vec::new();
    for &n in &nums {
      count[n as usize] += 1;
      if count[n as usize] == 2 {
        res.push(n);
      }
    }
    res
  }
}