1 00:00:00,900 --> 00:00:01,680 ‫Welcome back. 2 00:00:01,680 --> 00:00:06,750 ‫And the last video we checked out hash tables and now I have a little challenge for you. 3 00:00:06,750 --> 00:00:13,110 ‫So please go ahead and write a program that will iterate through each element of students of this array 4 00:00:13,110 --> 00:00:13,800 ‫that we have here. 5 00:00:13,800 --> 00:00:17,160 ‫Students array and insert them into a hash table. 6 00:00:17,310 --> 00:00:23,280 ‫If a student with a same ID already exists in the hash table, then skip it and display the following 7 00:00:23,310 --> 00:00:23,640 ‫error. 8 00:00:23,640 --> 00:00:26,850 ‫Sorry, a student with the same ID already exists. 9 00:00:26,850 --> 00:00:34,140 ‫And then as a little hint for you use the method contains key to check whether a student with the same 10 00:00:34,140 --> 00:00:35,760 ‫ID already exists. 11 00:00:36,120 --> 00:00:41,220 ‫So the program that we're looking at is going to have the same class student that we had in the last 12 00:00:41,220 --> 00:00:45,450 ‫video with an ID, the name GPA and these students here. 13 00:00:45,450 --> 00:00:47,130 ‫And then we have this. 14 00:00:48,390 --> 00:00:49,770 ‫Array of students. 15 00:00:50,370 --> 00:00:55,530 ‫So it has five entries and you can see that Louise has the same ID as Dennis, so that won't work. 16 00:00:56,190 --> 00:01:00,180 ‫But that is going to be the starting point of the challenge. 17 00:01:00,180 --> 00:01:03,540 ‫So please go ahead and post the video and try to solve the challenge. 18 00:01:05,850 --> 00:01:06,340 ‫Okay. 19 00:01:06,390 --> 00:01:07,680 ‫So I hope you tried it. 20 00:01:07,740 --> 00:01:12,620 ‫Basically, what we're going to need, of course, is going to be a hash table. 21 00:01:12,630 --> 00:01:16,200 ‫So let's first define our hash table like so. 22 00:01:16,440 --> 00:01:23,280 ‫And for this to work, of course, we need to make sure that we have using system collections in our 23 00:01:23,280 --> 00:01:24,930 ‫project at the top here. 24 00:01:25,620 --> 00:01:32,610 ‫So now that we have implemented this namespace, we can use hash tables in our project and now we will 25 00:01:32,610 --> 00:01:41,610 ‫need a for each loop that will go through our students array and add every single item of that array 26 00:01:41,610 --> 00:01:45,180 ‫to our hash table in case the ID doesn't exist already. 27 00:01:45,420 --> 00:01:51,150 ‫So therefore, we're going to use this for each loop which will check it out. 28 00:01:51,150 --> 00:01:57,540 ‫So it will go through each student inside of the student's list, so inside of the student's array, 29 00:01:57,540 --> 00:01:58,470 ‫so to speak. 30 00:01:58,770 --> 00:02:08,100 ‫And then it will check if the table that we're looking at doesn't already contain a certain key, so 31 00:02:08,100 --> 00:02:11,280 ‫contains key as dot ID. 32 00:02:12,270 --> 00:02:13,980 ‫So that will check this. 33 00:02:13,980 --> 00:02:17,160 ‫Basically, that's the table that we have here. 34 00:02:17,160 --> 00:02:23,220 ‫This one already have a key because there's always a key value here for a hash table. 35 00:02:23,220 --> 00:02:27,120 ‫So this contains key checks if the key already exists. 36 00:02:27,120 --> 00:02:33,480 ‫So for example, the key of one and if it doesn't exist already, then it will run this code. 37 00:02:33,480 --> 00:02:34,740 ‫But if it does. 38 00:02:35,990 --> 00:02:37,970 ‫Exist, then it will not run the code. 39 00:02:37,970 --> 00:02:40,220 ‫So it will skip this code here. 40 00:02:40,820 --> 00:02:46,940 ‫So then we can go to the else block and display the warning saying, Hey, this already exists. 41 00:02:47,390 --> 00:02:54,950 ‫So let's just write a little console statement here saying that this ID already exists and we just display 42 00:02:54,950 --> 00:02:57,470 ‫the ID itself as well as the ID here. 43 00:02:58,010 --> 00:03:04,330 ‫So now how can we actually add an item to our table list here? 44 00:03:04,340 --> 00:03:06,530 ‫So to our hash table, so to speak? 45 00:03:06,920 --> 00:03:12,340 ‫Well, we can just go ahead and say table dot ad, and then what do we add? 46 00:03:12,350 --> 00:03:15,400 ‫Well, we add the student, but where do we add it? 47 00:03:15,410 --> 00:03:21,170 ‫Well, we add it as as dot ID and we add as itself. 48 00:03:21,620 --> 00:03:28,520 ‫And then we can, of course also say that we added this student with the specific ID that we are looking 49 00:03:28,520 --> 00:03:28,940 ‫at. 50 00:03:29,910 --> 00:03:30,090 ‫Okay. 51 00:03:30,320 --> 00:03:31,890 ‫And that's basically going to be it. 52 00:03:31,910 --> 00:03:34,430 ‫So that's the solution to the challenge. 53 00:03:34,430 --> 00:03:41,690 ‫Basically this for each loop that checks if an ID already exists, if it does exist, then it goes here. 54 00:03:41,690 --> 00:03:48,530 ‫If it doesn't exist, then it goes here because here with this exclamation mark, we are saying not 55 00:03:48,530 --> 00:03:49,370 ‫so to speak. 56 00:03:49,370 --> 00:03:52,100 ‫So we're saying not contains key. 57 00:03:52,100 --> 00:03:54,470 ‫So if it does not contain the key, add it. 58 00:03:54,470 --> 00:03:57,470 ‫If it does contain the key, write this statement. 59 00:03:57,470 --> 00:03:57,770 ‫Sorry. 60 00:03:57,770 --> 00:03:59,810 ‫A student with the same id already exist. 61 00:04:00,620 --> 00:04:01,070 ‫Okay. 62 00:04:01,070 --> 00:04:07,520 ‫So let's run this code and check if this is actually going to work and we can see student with ID, 63 00:04:07,520 --> 00:04:12,950 ‫one was added, student with ID two was added, student with ID, six was added and then sorry, a student 64 00:04:12,950 --> 00:04:16,040 ‫with the same ID already exists and that's ID one. 65 00:04:16,040 --> 00:04:23,300 ‫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 66 00:04:23,300 --> 00:04:23,750 ‫as well. 67 00:04:23,750 --> 00:04:27,020 ‫So sorry, please, I took your ID. 68 00:04:27,050 --> 00:04:31,940 ‫So now of course we could write a code that will assign a different ID to her. 69 00:04:31,940 --> 00:04:34,940 ‫That's something that you could now try for yourself. 70 00:04:34,940 --> 00:04:42,680 ‫So in case that ID exists already, assign a different ID to that student and then go to the next iteration 71 00:04:42,680 --> 00:04:44,060 ‫of your for each loop. 72 00:04:44,570 --> 00:04:45,080 ‫All right. 73 00:04:45,080 --> 00:04:51,980 ‫So I hope you could solve this challenge yourself without the help of my solution. 74 00:04:51,980 --> 00:04:55,130 ‫Of course, there are different ways to get to a solution. 75 00:04:55,130 --> 00:04:56,690 ‫This is just one of the ways. 76 00:04:56,690 --> 00:04:59,720 ‫I hope you enjoyed it and see you in the next video.