Skip to content

Commit 6124ab2

Browse files
authored
Merge pull request neetcode-gh#902 from FahadulShadhin/c-solutions
Adding 217-Contains-Duplicate.c
2 parents e789410 + 78f1928 commit 6124ab2

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

c/217-Contains-Duplicate.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Time: O(n)
3+
Space: O(1)
4+
*/
5+
6+
typedef struct {
7+
int key;
8+
UT_hash_handle hh; // Makes this structure hashable
9+
} hash_table;
10+
11+
hash_table *hash = NULL, *elem, *tmp;
12+
13+
bool containsDuplicate(int* nums, int numsSize){
14+
if (numsSize == 1) {
15+
return false;
16+
}
17+
18+
bool flag = false;
19+
20+
for (int i=0; i<numsSize; i++) {
21+
HASH_FIND_INT(hash, &nums[i], elem);
22+
23+
if(!elem) {
24+
elem = malloc(sizeof(hash_table));
25+
elem->key = nums[i];
26+
HASH_ADD_INT(hash, key, elem);
27+
28+
flag = false;
29+
}
30+
else {
31+
flag = true;
32+
break;
33+
}
34+
}
35+
36+
// Free up the hash table
37+
HASH_ITER(hh, hash, elem, tmp) {
38+
HASH_DEL(hash, elem); free(elem);
39+
}
40+
41+
return flag;
42+
}

0 commit comments

Comments
 (0)