1
Fork 0
leetcode/source/set_mismatch/mod.rs

32 lines
644 B
Rust
Raw Permalink Normal View History

2022-04-12 13:26:20 +00:00
pub fn find_error_nums(numbers: Vec<i32>) -> Vec<i32> {
// Uncomment FromIterator in Leetcode since they're using Rust Edition 2018.
// Edition 2021 has this trait in its prelude.
// use std::iter::FromIterator;
use std::collections::HashSet;
let range = 1..=numbers.len() as i32;
let mut numbers_set = HashSet::<_>::new();
let mut duplicate = 0;
for number in numbers {
if numbers_set.insert(number) {
continue;
}
duplicate = number;
}
let mut missing = 0;
for number in range {
if !numbers_set.contains(&number) {
missing = number;
break;
}
}
vec![duplicate, missing]
}