Skip to content

Commit

Permalink
Merge pull request neetcode-gh#902 from FahadulShadhin/c-solutions
Browse files Browse the repository at this point in the history
Adding 217-Contains-Duplicate.c
  • Loading branch information
Ahmad-A0 authored Aug 23, 2022
2 parents e789410 + 78f1928 commit 6124ab2
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions c/217-Contains-Duplicate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}

0 comments on commit 6124ab2

Please sign in to comment.