Skip to content

Commit 5c2ee64

Browse files
committed
feat: add contains duplicate problem solution
1 parent 0f176f1 commit 5c2ee64

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

src/easy/contains_duplicate.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![allow(dead_code)]
2+
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
3+
use std::collections::HashSet;
4+
let mut set = HashSet::new();
5+
for num in nums {
6+
if set.contains(&num) {
7+
return true;
8+
}
9+
set.insert(num);
10+
}
11+
false
12+
}
13+
14+
/*
15+
Algorithm - Hash Set
16+
- Create a hash set
17+
- Iterate through the vector
18+
- If the hash set contains the current number
19+
- Return true
20+
- Else
21+
- Insert the current number into the hash set
22+
- Return false
23+
24+
Time Complexity = O(n)
25+
Space Complexity = O(n)
26+
*/
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use super::*;
31+
32+
#[test]
33+
fn test_contains_duplicate() {
34+
assert_eq!(contains_duplicate(vec![1, 2, 3, 1]), true);
35+
assert_eq!(contains_duplicate(vec![1, 2, 3, 4]), false);
36+
assert_eq!(contains_duplicate(vec![1, 1, 1, 3, 3, 4, 3, 2, 4, 2]), true);
37+
}
38+
}

src/easy/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod add_binary;
22
pub mod balanced_binary_tree;
33
pub mod binary_tree_inorder_traversal;
44
pub mod climbing_stairs;
5+
pub mod contains_duplicate;
56
pub mod convert_sorted_array_to_binary_search_tree;
67
pub mod implement_strstr;
78
pub mod length_of_last_word;
@@ -24,4 +25,4 @@ pub mod search_insert_position;
2425
pub mod sqrt_x;
2526
pub mod symmetric_tree;
2627
pub mod two_sum;
27-
pub mod valid_parentheses;
28+
pub mod valid_parentheses;

src/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
- [x] [112. Path sum](src/easy/path_sum.rs)
3131
- [x] [118. Pascal's triangle](src/easy/pascals_triangle.rs)
3232
- [x] [119. Pascal's triangle II](src/easy/pascals_triangle.rs)
33+
- [x] [217. Contains duplicate](src/easy/contains_duplicate.rs)
3334
- [x] [234. Palindrome linked list](src/easy/palindrome_linked_list.rs)
3435
- [ ] [Medium](src/medium)
3536
- [x] [2. Add two numbers](src/medium/add_two_numbers.rs)

theory/categories/1.arrays_&_hashing/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
6565

6666
## Problems
6767

68-
- [ ] [1. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | Easy problem | [Solution](src/easy/contains_duplicate.rs)
68+
- [x] [1. Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | Easy problem | [Solution](src/easy/contains_duplicate.rs)
6969
- [ ] [2. Valid Anagram](https://leetcode.com/problems/valid-anagram/) | Easy problem
70-
- [x] [3. Two Sum](https://leetcode.com/problems/two-sum/) [Solution](src/easy/two_sum.rs) | Easy problem
70+
- [x] [3. Two Sum](https://leetcode.com/problems/two-sum/) | Easy problem | [Solution](src/easy/two_sum.rs)
7171
- [ ] [4. Group Anagrams](https://leetcode.com/problems/group-anagrams/) | Medium Problem
7272
- [ ] [5. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | Medium Problem
7373
- [ ] [6. Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | Medium Problem

0 commit comments

Comments
 (0)