struct hashTable
{
int key;
int value;
UT_hash_handle hh;
};
int singleNumber(int* nums, int numsSize)
{
struct hashTable *hashtable = NULL;
for (int i = 0; i < numsSize; i++)
{
struct hashTable *tmp;
HASH_FIND_INT(hashtable, nums + i, tmp);
if (tmp)
{
tmp->value++;
}
else
{
struct hashTable *tmp = malloc(sizeof(struct hashTable));
tmp->key = nums[i];
tmp->value = 1;
HASH_ADD_INT(hashtable, key, tmp);
}
}
struct hashTable *res;
struct hashTable *tmp;
HASH_ITER(hh, hashtable, res, tmp)
{
if (res->value == 1)
{
return res->key;
}
}
return 0;
}
注:用到了Hash函数,所以要加上头文件uthash.h