A curated collection of Data Structures and Algorithms implemented in Rust, focused on clarity, correctness, and performance.
1use std::collections::HashSet;
2
3pub fn contains_duplicate(nums: Vec<i32>) -> bool {
4 let mut hash = HashSet::new();
5 for num in nums {
6 if hash.contains(&num) {
7 return true;
8 }
9 hash.insert(num);
10 }
11 return false;
12}
13
14#[cfg(test)]
15mod tests {
16 use crate::array_hashing::contains_duplicate::contains_duplicate;
17
18 #[test]
19 fn case_1() {
20 assert_eq!(contains_duplicate(vec![1, 2, 3, 1]), true);
21 assert_eq!(contains_duplicate(vec![1, 2, 3, 4]), false);
22 assert_eq!(contains_duplicate(vec![1, 1, 1, 3, 3, 4, 3, 2, 4, 2]), true);
23 }
24}