C solution, sort and see


  • 0
    V

    Yup, nothing smart here. Just sort the array and see if two adjacent elements are the same.

    int compar(const void * i1, const void * i2);
    
    bool containsDuplicate(int * nums, int numsSize)
    {
    	int i, j;
    	
    	qsort(nums, numsSize, sizeof(nums[0]), compar);
    	
    	for (i = 0, j = numsSize - 1; i < j; ++i)
    		if (nums[i] == nums[i + 1])
    			return true;
    		
    	return false;
    }
    
    int compar(const void * i1, const void * i2)
    {
    	return *(const int *)i1 - *(const int *)i2;
    }
    

Log in to reply
 

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