devxlogo

Hash Tables

Hash Tables

Question:
How can I implement a hash table such that each “bucket” is associated with two independent keys?I need to be able to store two data per bucket that are associated with each other. In one case, I’m given one of the values and will need to return it’s corresponding value. In the other case, it’s just the opposite. So, I need to be able to search with each of the two datum at one point acting as the key. Is this possible with a hash table? And if so, I how do I implement this?I have already consulted the recommended texts (Algorithms in C++ by Robert Sedgewick and Practical Algorithms in C++ by Brian Flamig).Neither seem to address this issue.

Answer:
This is not possible unless you know in advance what your data set will be. If you knew that, you might get by without using a hash table at all.

It sounds like what you really want is a simple structure that contains both a key and value, and then find an algorithm for looking up the key and return the corresponding value.

I can’t very well provide more details, however, without knowing the nature of the data you want to store.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist