diff options
Diffstat (limited to 'other/contains_duplicate2.rs')
-rw-r--r-- | other/contains_duplicate2.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/other/contains_duplicate2.rs b/other/contains_duplicate2.rs new file mode 100644 index 0000000..b016e21 --- /dev/null +++ b/other/contains_duplicate2.rs @@ -0,0 +1,21 @@ +use std::collections::HashMap; +struct Solution; + +impl Solution { + pub fn contains_nearby_duplicate(nums: Vec<i32>, k: i32) -> bool { + let mut hm = HashMap::new(); + for (i, n) in nums.iter().enumerate() { + if let Some(j) = hm.insert(n, i) { + if (i as i32) - (j as i32) <= k { + return true; + } + } + } + false + } +} + +fn main() { + let nums = vec![1,2,3,1]; + print!("{}", Solution::contains_nearby_duplicate(nums, 3)); +} |