Very simple C solution


  • 7
    O

    Sort and search

    int comp (const void * a, const void * b) {
       return ( *(int*)a - *(int*)b );
    }
    
    
    bool containsDuplicate(int* nums, int numsSize) {
        // Sort
        qsort(nums, numsSize, sizeof(int), comp);
        
        // Loop
        for (int i = 0; i < numsSize - 1; i++) {
            if (nums[i] == nums[i+1]) return true;
        }
        
        return false;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.