‫Welcome back.
‫And the last video we checked out hash tables and now I have a little challenge for you.
‫So please go ahead and write a program that will iterate through each element of students of this array
‫that we have here.
‫Students array and insert them into a hash table.
‫If a student with a same ID already exists in the hash table, then skip it and display the following
‫error.
‫Sorry, a student with the same ID already exists.
‫And then as a little hint for you use the method contains key to check whether a student with the same
‫ID already exists.
‫So the program that we're looking at is going to have the same class student that we had in the last
‫video with an ID, the name GPA and these students here.
‫And then we have this.
‫Array of students.
‫So it has five entries and you can see that Louise has the same ID as Dennis, so that won't work.
‫But that is going to be the starting point of the challenge.
‫So please go ahead and post the video and try to solve the challenge.
‫Okay.
‫So I hope you tried it.
‫Basically, what we're going to need, of course, is going to be a hash table.
‫So let's first define our hash table like so.
‫And for this to work, of course, we need to make sure that we have using system collections in our
‫project at the top here.
‫So now that we have implemented this namespace, we can use hash tables in our project and now we will
‫need a for each loop that will go through our students array and add every single item of that array
‫to our hash table in case the ID doesn't exist already.
‫So therefore, we're going to use this for each loop which will check it out.
‫So it will go through each student inside of the student's list, so inside of the student's array,
‫so to speak.
‫And then it will check if the table that we're looking at doesn't already contain a certain key, so
‫contains key as dot ID.
‫So that will check this.
‫Basically, that's the table that we have here.
‫This one already have a key because there's always a key value here for a hash table.
‫So this contains key checks if the key already exists.
‫So for example, the key of one and if it doesn't exist already, then it will run this code.
‫But if it does.
‫Exist, then it will not run the code.
‫So it will skip this code here.
‫So then we can go to the else block and display the warning saying, Hey, this already exists.
‫So let's just write a little console statement here saying that this ID already exists and we just display
‫the ID itself as well as the ID here.
‫So now how can we actually add an item to our table list here?
‫So to our hash table, so to speak?
‫Well, we can just go ahead and say table dot ad, and then what do we add?
‫Well, we add the student, but where do we add it?
‫Well, we add it as as dot ID and we add as itself.
‫And then we can, of course also say that we added this student with the specific ID that we are looking
‫at.
‫Okay.
‫And that's basically going to be it.
‫So that's the solution to the challenge.
‫Basically this for each loop that checks if an ID already exists, if it does exist, then it goes here.
‫If it doesn't exist, then it goes here because here with this exclamation mark, we are saying not
‫so to speak.
‫So we're saying not contains key.
‫So if it does not contain the key, add it.
‫If it does contain the key, write this statement.
‫Sorry.
‫A student with the same id already exist.
‫Okay.
‫So let's run this code and check if this is actually going to work and we can see student with ID,
‫one was added, student with ID two was added, student with ID, six was added and then sorry, a student
‫with the same ID already exists and that's ID one.
‫And that is going to be well, I had the ID or Dennis had the idea of one and he said the idea of one
‫as well.
‫So sorry, please, I took your ID.
‫So now of course we could write a code that will assign a different ID to her.
‫That's something that you could now try for yourself.
‫So in case that ID exists already, assign a different ID to that student and then go to the next iteration
‫of your for each loop.
‫All right.
‫So I hope you could solve this challenge yourself without the help of my solution.
‫Of course, there are different ways to get to a solution.
‫This is just one of the ways.
‫I hope you enjoyed it and see you in the next video.