Interview Question
i have a doubt..
using hashtable, how do we get to know the first element of the array to be non repeating?
eg, array- e e a i r a
in hash table we have it as-
a-a
e-e
i
r
ans shud be 'e'. but we have 'a' in the hash table..
One more solution ...
Assume all letters ... use hash of 26 locations initial all locations as zero
1. Scan the array,
if(hashArray[arr[i]-65]==0)
hashArray[arr[i]-65] = i;
else
hashArray[arr[i]-65] = -1 ;
2. Scan the hasharray and find the min amongst elements excluding 0 and -1. say min
3. print arr[min]
Basically this logic can be expanded furthur to other range of characters just that the hash array to search will be bigger
One more solution ...
Assume all letters ... use hash of 26 locations initial all locations as zero
1. Scan the array,
if(hashArray[arr[i]-65]==0)
hashArray[arr[i]-65] = i;
else
hashArray[arr[i]-65] = -1 ;
2. Scan the hasharray and find the min amongst elements excluding 0 and -1. say min
3. print arr[min]
Basically this logic can be expanded furthur to other range of characters just that the hash array to search will be bigger
Use a bitHash . use a bool vector . initialise it with zero . for every char found we toggle that bit . If in course of iteration we are trying to switch a bit off , we have found the char .
t.c -O(n)
s.c- very less : 256 bits or 8 int instead of 256 int arr.
use hash table
- xmagics September 13, 2008