forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path217-Contains-Duplicate.c
More file actions
42 lines (33 loc) · 808 Bytes
/
217-Contains-Duplicate.c
File metadata and controls
42 lines (33 loc) · 808 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
40
41
42
/*
Time: O(n)
Space: O(1)
*/
typedef struct {
int key;
UT_hash_handle hh; // Makes this structure hashable
} hash_table;
hash_table *hash = NULL, *elem, *tmp;
bool containsDuplicate(int* nums, int numsSize){
if (numsSize == 1) {
return false;
}
bool flag = false;
for (int i=0; i<numsSize; i++) {
HASH_FIND_INT(hash, &nums[i], elem);
if(!elem) {
elem = malloc(sizeof(hash_table));
elem->key = nums[i];
HASH_ADD_INT(hash, key, elem);
flag = false;
}
else {
flag = true;
break;
}
}
// Free up the hash table
HASH_ITER(hh, hash, elem, tmp) {
HASH_DEL(hash, elem); free(elem);
}
return flag;
}