# C solution beats 97%

• ``````bool containsNearbyDuplicate(int* nums, int numsSize, int k) {
if(numsSize ==0) return false;
if(k>numsSize) k= k% numsSize;
int i, min= INT_MAX, max = INT_MIN;
for(i=0; i<numsSize; i++){
min = min< nums[i]? min: nums[i];
max = max> nums[i]? max: nums[i];
}
int *hash = malloc((max-min+1)* sizeof(int));
memset(hash, -1, (max-min+1)*sizeof(int));
for(i=0; i<numsSize; i++){
if(hash[nums[i]-min] == -1){
hash[nums[i]-min] = i;
}
else if((i-hash[nums[i]-min]) <= k){
return true;
}
else{
hash[nums[i]-min] = i;
}
}
return false;
}``````

• Doesn't seem to be a good idea.
The max range of int on a modern machine is usually ~2^32 meaning that your code could take up to 2^(32-30) * 4 = 16 Gigabyte of memory. You could save some memory by using an array of bool (4G) or even std::bitset (0.5G) which, however, is still bad.

• Since C does not have a hash table lib function, you would have to write one (only specific to this problem, not generic) yourself with small hash table size that can handle collision.

• I understand and agree to what you said but I dont understand why you said it. I was just saying that the op's solution takes up too much memory.