using hash table to spped up algorithm