Four Semesters of Computer Science in 5 Hours, Part 2

# Hashing Functions Explained

Check out a free preview of the full Four Semesters of Computer Science in 5 Hours, Part 2 course:
The "Hashing Functions Explained" Lesson is part of the full, Four Semesters of Computer Science in 5 Hours, Part 2 course featured in this preview video. Here's what you'd learn in this lesson:

## How a string gets transformed from a string to a number which corresponds to the index in an array.

Get Unlimited Access Now

Transcript from the "Hashing Functions Explained" Lesson

[00:00:00]
>> Brian Holt: Cool, so let's take a look at the exercise. Yeah, Mark.
>> Mark: So these hashing functions could give the same values even for different key values?
>> Brian Holt: Yeah. So that's actually probably a good point to bring up. Let's just quickly just show you what we mean there. So I'm gonna have some array of, let's just say 5 for sake of brevity.

[00:00:32]
>> Brian Holt: So what's cool about hashing functions is that the item that you're trying to store in this particular array is also the key, right? So it's the key of what's getting put in there, because you put the string into the hashing function, it gives you back the index.

[00:00:49] Now in this particular case, my index is only 5 long, right? So if it gives me a hash back of 13, right, I'm outside of the array and it's no longer useful, right? So what we're gonna do is we're basically going to run it through some of the hashing function.

[00:01:06] Let's say it gives me back 13, but then I'm just gonna run modulus, modulo, whatever you want to call that percentage sign. [LAUGH] Whatever the length of the array is. This is actually gonna give me back 3, which is going to be inside of that. Now if you're not familiar with this operator, it's available in just about every programming language.

[00:01:30] It's extremely important. But if you remember doing long division in school, you would say that this is two remainder three, right? If you were doing 13 divided by 5 what modulus does is it just ignores the integer division part of it, the first part of it, and gets you just the remainder back, right?

[00:01:50] So that way if run the modulus 5 through it you're always gonna get something inside of your array. Hopefully I answered that person's question.