Since you cannot use any extra space, exclude the use of another hash table.
Now, approaching the hashing of an existing array, it can be achieved if we are allowed to modify the array in place.
Algo:
1) Start with the first element.
2) Hash the first element and apply the conversion to the hash value. Let's say this conversion makes the value -ve.
3) Go to the next item. Hold the item and verify that the transform has been applied before applying the transform.
4) If yes, then the item is a duplicate.
the code:
for(i = 0; i < size; i++) { if(arr[abs(arr[i])] > 0) arr[abs(arr[i])] = -arr[abs(arr[i])]; else cout<< abs(arr[i]) <<endl; }
This conversion is necessary, because if we want to use a hash approach, then there must be a collision to hash the same key.
I don’t think about how hashing can be used without any extra space and not changing the array.
basav
source share