Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Pseudocoding set(), get(), & remove()" Lesson is part of the full, Data Structures and Algorithms in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Bianca pseudocodes the constructor, set(), get(), and remove() methods for a hash table. The constructor would initialize the storage object and hashing function. The get() and set() methods would use the hashing function to access the specified data from the storage.

Get Unlimited Access Now

Transcript from the "Pseudocoding set(), get(), & remove()" Lesson

>> Bianca Gandolfo: We're gonna spend ten minutes pseudocoding out these operations, not the hashing function though. You can just assume that the hashing function works under some rules that you create, right? The rule probably is the number needs to be returned between 0 and 10 or something. You wanna have a rule in turn, because you don't want it to return 352 when you wanna work with an array that's size 10.

>> Bianca Gandolfo: Cool.
>> Speaker 2: Sorry, I'm still confused. Where would the hash take place with us implementing this cuz we can just assign a key and a value to an object, right, without-
>> Bianca Gandolfo: We're using an array though and our hash is gonna hash to an index.
>> Speaker 2: Okay, I see.

[00:00:45] Okay, thank you.
>> Bianca Gandolfo: Yeah, yeah. We had a task. Our task was pseudo,
>> Bianca Gandolfo: Code out.
>> Speaker 3: [INAUDIBLE] Size, please.
>> Bianca Gandolfo: Out a hash table, all right. So we have our constructor. What do we need inside of our constructor?
>> Speaker 4: Storage.
>> Bianca Gandolfo: Some sort of storage. What did you guys use for your storage?

>> Speaker 4: An array.
>> Bianca Gandolfo: Some empty array. And then you also want a hashingFunction,
>> Bianca Gandolfo: Right? That's gonna take some value and return,
>> Bianca Gandolfo: An index for the array, right? So we can initialize our array to size ten.
>> Bianca Gandolfo: So we'll just have an array. Let's do five to save some typing here.

>> Bianca Gandolfo: So here we are. That should say undefined. So we're initializing our array to size five and this is our hash table. And we have a hashing function and it's going to return index for the array. Specifically what that means is gonna return a number between 0 and 4, right?

[00:02:23] Cuz our hashing function actually doesn't know anything about the array. It just knows that it has to return a number, an integer, in between zero and four. And that's a constraint that we can give to our hash function and hash functions tend to be flexible, and we'll talk about why in just a second.

[00:02:42] So here's our hash function. So then we had a few methods. We had a setter, we had a getter and then we had a remove, right?
>> Bianca Gandolfo: So to set, we're gonna take a key and a value and we want to save the value, save the value in the array, right?

[00:03:09] And for that to happen, we somehow have to get our key, which might be a string, right? What was an example that we used earlier, thingamajig?
>> Speaker 2: Yes.
>> Bianca Gandolfo: Thingamajig is our key and our value was true.
>> Bianca Gandolfo: We now wanna make this saved into our storage. So how do we do that?

[00:03:37] So we say the value in the array, so we need to run the hash function,
>> Bianca Gandolfo: With only the key, thingamajig.
>> Bianca Gandolfo: Gonna run it. This is going to return,
>> Bianca Gandolfo: Some value. Let's say it returns 4, let's say it returns 3, actually.
>> Bianca Gandolfo: And then you want to save true which is our value to the third index of our storage,

>> Bianca Gandolfo: Cool? So that's how we are saving properties and values into our hash table. Any questions about that?
>> Bianca Gandolfo: Cool, yeah?
>> Speaker 5: In the storage, what is the difference using undefined or null?
>> Bianca Gandolfo: What's the difference between undefined and null?
>> Speaker 5: What if we use that-
>> Bianca Gandolfo: What if we use null?

>> Speaker 5: Yeah.
>> Bianca Gandolfo: So when you initialize an array, it's gonna initialize it to undefined, it's not gonna initialize it to null. For you to initialize each value to null, you'd have to loop through the array and set it to null. But if you initialize an array on its own, it's just automatically gonna have an empty array with a bunch of undefineds in it.

>> Bianca Gandolfo: Yeah,
>> Bianca Gandolfo: Okay, so we're gonna have a getter. What do we pass to our getter?
>> Speaker 4: You pass the key.
>> Bianca Gandolfo: Yeah, we're gonna pass the key.
>> Bianca Gandolfo: And the thing that we wanna do here is you wanna return the value, right?
>> Speaker 4: Yeah.
>> Bianca Gandolfo: Value saved in storage.

[00:05:44] So what do we need to do to get there?
>> Speaker 4: We have to run the hashing function on the key again.
>> Bianca Gandolfo: Yep.
>> Speaker 4: And because it's a hashing function it'll return the same value that it did the first time.
>> Bianca Gandolfo: Mm-hm.
>> Speaker 4: Which will give you the address in the array.

>> Bianca Gandolfo: It's gonna return the same value.
>> Bianca Gandolfo: So for example, 3 for thingamajig.
>> Bianca Gandolfo: Right, and then with that value we can just go directly,
>> Bianca Gandolfo: We can go directly to our storage. Retrieve value from storage using the index from the hash function.
>> Bianca Gandolfo: Cool, does that make sense?

>> Bianca Gandolfo: Great.
>> Bianca Gandolfo: So, remove. So say that we no longer wanna have a thingamajig,
>> Bianca Gandolfo: Equals true in our hash table. Cuz we're like, wow, that's the least useful thing to have in our hash table, we need to get it out. How do we do that?
>> Speaker 2: Can we just set the,

>> Speaker 2: The value to that key to null?
>> Bianca Gandolfo: Yeah, so when we remove it, we're gonna pass the key again, right?
>> Bianca Gandolfo: So we wanna set the value at the index to null, maybe undefined, whatever, something like that. How do we get the index?
>> Speaker 2: Hash the key.

>> Bianca Gandolfo: Yep, hash the key to get the index. And then, look up index on storage, set that to null, cool.