-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontains_duplicate.rs
More file actions
39 lines (34 loc) · 889 Bytes
/
Copy pathcontains_duplicate.rs
File metadata and controls
39 lines (34 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#![allow(dead_code)]
use std::collections::HashSet;
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
let mut set = HashSet::new();
for num in nums {
if set.contains(&num) {
return true;
}
set.insert(num);
}
false
}
/*
Algorithm - Hash Set
- Create a hash set
- Iterate through the vector
- If the hash set contains the current number
- Return true
- Else
- Insert the current number into the hash set
- Return false
Time Complexity = O(n)
Space Complexity = O(n)
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_contains_duplicate() {
assert_eq!(contains_duplicate(vec![1, 2, 3, 1]), true);
assert_eq!(contains_duplicate(vec![1, 2, 3, 4]), false);
assert_eq!(contains_duplicate(vec![1, 1, 1, 3, 3, 4, 3, 2, 4, 2]), true);
}
}