int* findErrorNums(int* nums, int numsSize, int* returnSize) { *returnSize = 2;
int* ans = malloc(sizeof(int) * 2);
struct entry *hash = NULL;
// hash all values, find duplicate value: -- LOOP A --
for (int i=0; i < numsSize; i++) {
struct entry *e;
int num = nums[i];
HASH_FIND_INT(hash, &num, e);
if (e) ans[0] = e->id; // value already exists, mark it
else {
struct entry *new = malloc(sizeof(struct entry));
new->id = nums[i];
HASH_ADD_INT(hash, id, new);
}
}
// search for non-existent value: -- LOOP B --
for (int i=0; i < numsSize; i++) {
struct entry *e;
int num = nums[i];
HASH_FIND_INT(hash, &num, e);
if (!e) {
ans[1] = nums[i]; // value does not exist, mark it
break;
}
}
// delete / free memory: -- LOOP C --
struct entry *cur, *tmp;
HASH_ITER(hh, hash, cur, tmp) {
HASH_DEL(hash, cur);
free(cur);
}
return ans;
}