You are given one fixed list of numbers (let’s call it the first list) and one other list (second list). How can you efficiently find out if there is any element in the second list that is an element of the first list?
You are given one fixed list of numbers (let’s call it the first list) and one other list (second list).
How can you efficiently find out if there is any element in the second list that is an element of
the first list?
Hash each of the numbers in the fixed list to a hash table using a function that will return a
distinct hash value for each of the numbers in this fixed list (size of the hash table itself is irrelevant,
as long as it is larger than the number of numbers to be put in it).
In the second list, for each value, determine the hash value. Compare this to the number in that
location in the hash table. If the number is the same, the element is in both lists. If the number in
that location is different or does not exist, then the element in the second list is not in the first list.
How can you efficiently find out if there is any element in the second list that is an element of
the first list?
Hash each of the numbers in the fixed list to a hash table using a function that will return a
distinct hash value for each of the numbers in this fixed list (size of the hash table itself is irrelevant,
as long as it is larger than the number of numbers to be put in it).
In the second list, for each value, determine the hash value. Compare this to the number in that
location in the hash table. If the number is the same, the element is in both lists. If the number in
that location is different or does not exist, then the element in the second list is not in the first list.
Comments
Post a Comment
https://gengwg.blogspot.com/