Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
最直接的思路,o(n*n)

bool containsDuplicate(int* nums, int numsSize) 
{
    for(int i=0;i<numsSize;i++)
    {
        for(int j=i+1;j<numsSize;j++)
        {
            if(nums[i] == nums[j])
                return true;
        }
    }
    
    return false;
    
}

然后另一种思路可以先排序再遍历一遍。
C++中可以借助set来AC。

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        return nums.size() > unordered_set<int>(nums.begin(), nums.end()).size();        
    }
};

C语言的话可以借助哈希链表来实现,添加英文注释代码如下:

struct Node
{
    int val;
    struct Node *next;
};

struct Set
{
    int bucketSize;
    struct Node **table;
};

void initSet(struct Set *set, int bucketSize)
{
    set->bucketSize = bucketSize;
    set->table = (struct Node **)malloc(sizeof(struct Node*) * bucketSize);
    memset(set->table, 0, sizeof(struct Node*) * bucketSize);
}

bool addValue(struct Set *s, int val)
{
    int idx = 0;
    /*-INT_MIN overflow in expression -val*/
    if(INT_MIN == val)
        idx = 0;
    else
        idx = val > 0 ? val : -val;
    /*find which list to add*/ 
    idx %= s->bucketSize;
    struct Node *ptr = s->table[idx];
    while(ptr != NULL)
    {
        if(ptr->val == val)
        {
            return false;
        }
    
        ptr = ptr->next;
    }
    /*add new value,malloc memory,assign val*/
    ptr = malloc(sizeof(struct Node));
    ptr->val = val;
    /*update the new added value ,as the first in list*/
    ptr->next = s->table[idx];
    s->table[idx] = ptr;
    return true;
}

void releaseSet(struct Set *s)
{
    struct Node *ptr, *tmp;
    for(int i = 0; i < s->bucketSize; ++i)
    {
        ptr = s->table[i];
        while(ptr != NULL)
        {
            tmp = ptr;
            ptr = ptr->next;
            free(tmp);
        }
    }
    free(s->table);
    s->table = NULL;
    s->bucketSize = 0;
}

bool containsDuplicate(int* nums, int numsSize) 
{
    if(numsSize < 2)
    {
        return false;
    }
    
    struct Set set;
    
    /*here numsSize/2 && numsSize both ok,if divide > 2,
    when array has less than divide,bucketSize is zero
    idx %= s->bucketSize wrong : division by zero*/
    initSet(&set, numsSize/2);
    
    for(int i = 0; i < numsSize; ++i)
    {
        if(!addValue(&set, nums[i]))
        {
            /*free memory*/
            releaseSet(&set);
            return true;
        }
    }
    releaseSet(&set);
    return false;
}