WEBVTT 0 00:01.100 --> 00:02.630 Welcome! In this lecture, 1 00:02.630 --> 00:06.320 I'm gonna show you a program which can print unique random numbers. 2 00:06.320 --> 00:08.910 First I'm going to build it by using arrays. 3 00:08.930 --> 00:11.150 Then I'm going to refactor it into slices. 4 00:11.540 --> 00:13.310 Okay let's get started. 5 00:13.310 --> 00:17.210 Let's seed the rand package first. 6 00:17.330 --> 00:20.620 I want to create five random numbers. To make it clear, 7 00:20.630 --> 00:30.010 let's put it into a constant. OK. Let's generate random numbers using a loop. Here, 8 00:30.040 --> 00:33.500 I'm going to generate a random number like this. 9 00:33.620 --> 00:36.400 OK let me print it as well.. 10 00:36.450 --> 00:41.940 All right let me run it. As you can see, sometimes the numbers are duplicated, right? 11 00:42.000 --> 00:42.350 OK. 12 00:42.360 --> 00:49.800 Now let's search for the unique numbers. I can store the unique numbers in an array like this one. Now, 13 00:49.840 --> 00:57.480 I'm going to loop over the array like this and here I need to check whether the generated random number 14 00:57.480 --> 01:00.930 is a duplicate or not. If it's a duplicate, 15 01:00.970 --> 01:05.060 Then, let's continue to generate more random numbers. By the way, 16 01:05.110 --> 01:11.140 whenever the program finds a duplicate number, it should generate a new random number instead. To do that, 17 01:11.140 --> 01:11.530 To do that, 18 01:11.530 --> 01:14.540 It needs to continue from the parent loop. 19 01:14.700 --> 01:22.800 So I'm going to label the parent loop like this. Then, I'm going to continue from the parent loop. OK. 20 01:22.880 --> 01:27.430 If the execution reaches here, it means that the number is unique. 21 01:27.530 --> 01:36.100 So I'm going to save it into the array, and now I need to move this incdec statement from here to 22 01:36.100 --> 01:42.000 here. So that it will be incremented only when a unique number is found. 23 01:42.010 --> 01:42.270 OK. 24 01:42.280 --> 01:50.300 Now let's print the unique numbers once right after the loop. 25 01:50.310 --> 01:58.310 Now it finds the unique numbers. Perfect! Let's make the program dynamic by getting this max value from 26 01:58.310 --> 01:59.780 the command line. 27 01:59.780 --> 02:04.730 For now I'm going to skip the error handling, but you usually should always handle the errors. 28 02:05.050 --> 02:05.870 OK. 29 02:06.030 --> 02:07.100 Here is an error. 30 02:07.100 --> 02:11.270 Remember I cannot declare the length of an array using a variable. 31 02:11.900 --> 02:16.400 OK let's change its size manually to 10 for now like this. 32 02:17.240 --> 02:20.950 OK let's try it ok. 33 02:20.960 --> 02:24.030 It works but the half of the array is empty. 34 02:24.050 --> 02:30.920 It's because my program always allocates an array with ten elements even though I only want five unique 35 02:30.920 --> 02:32.360 random numbers. 36 02:32.510 --> 02:36.740 Think of what would happen if the array had millions of elements? 37 02:36.740 --> 02:39.250 This would be a huge waste of space! 38 02:39.320 --> 02:40.380 Right? 39 02:40.430 --> 02:43.240 By the way this is not the only problem here. 40 02:43.250 --> 02:50.530 For example let's pass eleven this time. Now the program crashes because there isn't enough space in 41 02:50.530 --> 02:57.550 the array. So it is clear that I cannot make this program dynamic using an array. 42 02:57.610 --> 03:02.490 I need something dynamic something that can change its length dynamically. 43 03:02.530 --> 03:04.030 You know the answer. 44 03:04.120 --> 03:11.020 So now let's refactor this program to slices. First, I'm gonna remove the length from here. 45 03:11.020 --> 03:13.960 So the unique variable is not an array anymore. 46 03:13.960 --> 03:19.090 It's a slice variable now. Let me run it. Wow! It crashes again. 47 03:19.840 --> 03:21.650 Let me explain. Here, 48 03:21.670 --> 03:25.390 this statement tries to assign to a nil slice. 49 03:25.390 --> 03:29.380 That is why there is an indexing error here, because the slice is nil. 50 03:29.530 --> 03:32.350 So it doesn't contain any elements. 51 03:32.380 --> 03:39.070 So instead I'm going to use a built-in function called the append, let me comment out this statement 52 03:39.070 --> 03:40.060 first. 53 03:40.060 --> 03:42.940 Now I'm going to use the append function like this. 54 03:42.940 --> 03:48.220 This function appends the n variable to the unique slice as a new element. 55 03:48.220 --> 03:49.860 Okay let's try it. 56 03:49.870 --> 04:04.320 Let's past 10 or let's pass 15 or let's pass 25 or let's pass 50 as you can see it works. The slice dynamically 57 04:04.410 --> 04:12.180 adjusts its length. It grows value when you append a new element to it. By the way, I'll talk about the append function 58 04:12.210 --> 04:21.030 in detail afterward, no worries. OK. Now we can refactor this code. Let's start with this found variable. 59 04:21.300 --> 04:27.230 I don't need it anymore. That's because we can check the length of the uniques slice directly. 60 04:27.540 --> 04:33.240 This wasn't possible before with an array. It's because, as you know, the length of an array is fixed, however, 61 04:33.300 --> 04:38.050 a slice's length dynamically changes, so we can use it as in here. 62 04:38.160 --> 04:40.870 So let's get rid of it from here first. 63 04:40.920 --> 04:47.640 I'm also going to remove it from this loop as well, here. Instead, I'm going to get the length of the slice 64 04:47.640 --> 04:51.940 using our old friend the len function like this. 65 04:52.240 --> 04:56.690 Let's try running it again. Cool! It works! By the way, 66 04:56.690 --> 05:04.180 almost every function and method in the Go standard library prefer to work with slices instead of arrays. 67 05:04.190 --> 05:05.680 Let me show you why. 68 05:05.990 --> 05:08.990 Let's say I want to sort the unique numbers. 69 05:08.990 --> 05:12.770 There is a function to do that for me in the Go standard library. 70 05:12.770 --> 05:20.660 It's called sort.Ints. Let's take a look at its documentation. As you can see, it takes an int slice. 71 05:20.870 --> 05:28.070 So you can pass any int slice to this function no matter what the slice's length is. If this function were 72 05:28.070 --> 05:35.750 accepting an array instead, it would be harder to use this function. There would be hundreds of duplicate 73 05:35.750 --> 05:42.500 sort functions just to accept different lengths of arrays like these ones. For example, this function 74 05:42.500 --> 05:49.240 can on the work with an array that has 50 int elements or this one can only work with an array with a hundred 75 05:49.300 --> 05:57.090 int elements and so on. So, Instead of this, sort.Ints function expects a slice like this. 76 05:57.150 --> 06:01.800 This is superior because it can work with a slice of any length. 77 06:01.800 --> 06:05.820 Anyways let's pass the unique slice to this function. 78 06:05.880 --> 06:16.520 Let me also print it. Let's try it. As you can see, now the unique numbers are sorted. Cool! By the way, using 79 06:16.520 --> 06:17.420 this function, 80 06:17.420 --> 06:25.230 you can also sort an int array such as this one. Let me pass it to the Ints function. Let me also 81 06:25.230 --> 06:30.540 print the result. As you can see, it doesn't work, but why? 82 06:30.720 --> 06:33.910 It's because, the nums array's type is [5]int. 83 06:34.190 --> 06:40.160 But sort.Ints function expects an int slice. It doesn't expect an array. 84 06:40.310 --> 06:42.110 So what should I do? 85 06:42.110 --> 06:47.900 Well, I can convert this array to a slice by using a slice expression like this. 86 06:48.350 --> 06:56.200 Let's run it. Cool! It works! As you can see, an array can also be used like a slice. 87 06:56.390 --> 07:00.980 In this section, I'll talk about everything you see here in detail by the way. 88 07:00.980 --> 07:04.220 No worries. This was just another introduction. 89 07:04.310 --> 07:07.540 I hope you're beginning to understand the power of slices now. 90 07:07.550 --> 07:11.870 We are still exploring its shores. 91 07:11.870 --> 07:13.040 Thank you for watching. 92 07:13.040 --> 07:13.910 See you in the next lecture.