Skip to main content
Back to problems
#645
Easy Algorithms

Set mismatch

Array Hash Table Bit Manipulation Sorting
44.2% acceptance
Feb 20, 2026
5400
1354
Find the number that occurs twice and the number that is missing.

Solution

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