16 lines
535 B
Rust
16 lines
535 B
Rust
|
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)
|
||
|
}
|