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

16 lines
535 B
Rust
Raw Permalink Normal View History

2022-04-11 12:02:46 +00:00
pub fn contains_nearby_duplicate(numbers: Vec<i32>, range: i32) -> bool {
// Uncomment FromIterator in Leetcode since they're using Rust Edition 2018.
// Edition 2021 has this trait in its prelude.
// use std::iter::FromIterator;
fn contains_duplicate(numbers: &[i32]) -> bool {
std::collections::HashSet::<_>::from_iter(numbers).len() != numbers.len()
}
contains_duplicate(&numbers)
&& numbers
.as_slice()
.windows(std::cmp::min((range + 1) as usize, numbers.len()))
.any(contains_duplicate)
}