32 lines
644 B
Rust
32 lines
644 B
Rust
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]
|
|
}
|