1 00:00:01,180 --> 00:00:01,870 Hi, everyone. 2 00:00:01,900 --> 00:00:04,780 So today we are going to solve this question single no. 3 00:00:05,110 --> 00:00:07,090 OK, so what is the problem statement? 4 00:00:07,110 --> 00:00:08,820 So no statement is very clear. 5 00:00:09,070 --> 00:00:10,870 So we will be given. 6 00:00:10,870 --> 00:00:13,230 So the input will basically be Eddie. 7 00:00:14,490 --> 00:00:21,540 I ready and we have to do so, basically, every element will appear twice inside the area except for 8 00:00:21,540 --> 00:00:25,620 one element, and our task is to basically find that one element. 9 00:00:26,080 --> 00:00:28,270 OK, so let us consider some example. 10 00:00:28,560 --> 00:00:30,170 So this is our input today. 11 00:00:30,450 --> 00:00:34,060 So, see, every element is appearing twice except one element. 12 00:00:34,080 --> 00:00:36,490 So this is orcharding only one time. 13 00:00:36,510 --> 00:00:39,630 So our answer for this input will basically be one. 14 00:00:40,400 --> 00:00:44,760 OK, now C two is updating two times when is appearing two times. 15 00:00:44,760 --> 00:00:47,040 So forward is the element that we have to find. 16 00:00:47,070 --> 00:00:53,310 So in this case, our answer will be four, because every element is appearing twice except one element. 17 00:00:53,550 --> 00:00:56,040 And we have to find this one element. 18 00:00:56,280 --> 00:00:58,500 OK, now let see this example. 19 00:00:59,070 --> 00:01:02,550 So one is appearing two times, two is appearing two times. 20 00:01:02,760 --> 00:01:06,120 So I have this elementally, so these are being only one time. 21 00:01:06,140 --> 00:01:07,410 So our answer will be three. 22 00:01:07,770 --> 00:01:09,900 OK, so I hope you understood the question. 23 00:01:10,200 --> 00:01:12,790 Now let's discuss the approaches to solve this question. 24 00:01:12,860 --> 00:01:15,330 OK, let's discuss approach number one. 25 00:01:15,750 --> 00:01:19,230 OK, so first approach is basically it involves sorting. 26 00:01:20,550 --> 00:01:23,200 OK, so what we will do, so we will sort the area. 27 00:01:23,610 --> 00:01:23,890 OK. 28 00:01:23,910 --> 00:01:26,320 So after sorting the area, what will happen? 29 00:01:26,490 --> 00:01:28,500 So let us consider this example. 30 00:01:28,710 --> 00:01:29,020 OK. 31 00:01:29,040 --> 00:01:33,060 So this area will become one to two. 32 00:01:33,270 --> 00:01:34,670 So basically what will happen? 33 00:01:34,680 --> 00:01:38,730 So if the elements are slim, they will appear in a decent manner. 34 00:01:38,760 --> 00:01:43,900 OK, so in this case, I will to over the area and I can see when is the different element. 35 00:01:43,970 --> 00:01:48,300 OK, so first I will start the area and then I will iterate over the area. 36 00:01:48,600 --> 00:01:50,790 So what is the time and the space complexity. 37 00:01:51,090 --> 00:01:54,440 So spaces will be are drawn and time is first. 38 00:01:54,450 --> 00:01:59,790 I am starting the area so and Logan and then I armatrading out the area which is then. 39 00:01:59,790 --> 00:02:01,320 So basically this is nothing but. 40 00:02:01,830 --> 00:02:02,550 And Logan. 41 00:02:03,560 --> 00:02:05,600 OK, similarly, if you will sort this out. 42 00:02:05,750 --> 00:02:10,490 So what will happen, all the elements which are the same, they will appear in the they will come in. 43 00:02:10,490 --> 00:02:11,810 They are designed to be OK. 44 00:02:12,170 --> 00:02:16,450 So this idea will become one one, two, two and four. 45 00:02:16,670 --> 00:02:20,780 So basically, when you illustrate they are saying move overhead, they are saying move now. 46 00:02:20,780 --> 00:02:22,020 This is the different element. 47 00:02:22,040 --> 00:02:27,020 OK, so in this way, you can find out the element now for this area, if you will apply the starting 48 00:02:27,020 --> 00:02:27,920 approach, what will happen? 49 00:02:27,950 --> 00:02:30,860 So this will be one one, then again to two and then three. 50 00:02:31,340 --> 00:02:32,910 OK, so these two elements are same. 51 00:02:32,930 --> 00:02:33,860 These tremendous him. 52 00:02:34,100 --> 00:02:35,910 This is occurring one time. 53 00:02:35,930 --> 00:02:37,730 OK, so our answer will be this. 54 00:02:38,830 --> 00:02:46,010 So in this way, what we will do first start theory and then to find out the answer and just outraged 55 00:02:46,100 --> 00:02:48,470 over the error and find the unique element. 56 00:02:48,500 --> 00:02:52,580 OK, so this is the time and the space complexity time and again. 57 00:02:52,880 --> 00:02:55,290 And space complexity is basically one. 58 00:02:56,390 --> 00:02:59,070 OK, so now let us discuss the second approach. 59 00:02:59,360 --> 00:03:02,410 So the second approach, basically it is it uses Hashmat. 60 00:03:02,690 --> 00:03:09,020 OK, so in the second approach, what we do so basically in the second approach we will create a hash 61 00:03:09,020 --> 00:03:09,350 map. 62 00:03:10,900 --> 00:03:13,470 OK, so what we'll do, we will take a map. 63 00:03:13,510 --> 00:03:15,220 OK, so the key will be element. 64 00:03:17,360 --> 00:03:20,090 And the value will be basically the account of that element. 65 00:03:20,240 --> 00:03:23,300 OK, so this is the key value that I will totally Hashmat. 66 00:03:23,340 --> 00:03:25,650 OK, so now let's just iterate over the array. 67 00:03:25,940 --> 00:03:27,720 So let us consider this example. 68 00:03:27,740 --> 00:03:29,490 OK, four, one, two, one, two. 69 00:03:29,720 --> 00:03:33,710 So I have for one again, two, one and two. 70 00:03:34,430 --> 00:03:36,640 OK, so initially my map is empty. 71 00:03:36,650 --> 00:03:39,750 Now I trade so I have a limit for its count. 72 00:03:39,750 --> 00:03:40,190 This one. 73 00:03:40,500 --> 00:03:41,990 Then I have element one. 74 00:03:42,230 --> 00:03:45,400 So element one, it's count this one, then I have two. 75 00:03:45,620 --> 00:03:49,580 So I have element to then it's count this one, I have element one. 76 00:03:49,580 --> 00:03:51,950 So increase the count so it will become two. 77 00:03:52,340 --> 00:03:54,380 I have this element also includes the count. 78 00:03:54,380 --> 00:03:55,280 It will become too. 79 00:03:55,490 --> 00:03:59,570 OK, so once you will iterate over the array you hash map is ready. 80 00:04:00,590 --> 00:04:01,730 I would be ready. 81 00:04:01,730 --> 00:04:04,490 Then what we will do we will trade over the hash map. 82 00:04:04,520 --> 00:04:06,710 OK, we will trade over the hash map. 83 00:04:07,310 --> 00:04:13,010 And if there is an element present whose count is basically one, then our answer will be basically 84 00:04:13,040 --> 00:04:13,550 the key. 85 00:04:13,620 --> 00:04:17,209 OK, so in this case I would answer well before because it's count this one. 86 00:04:17,730 --> 00:04:19,959 OK, now let us consider one more example. 87 00:04:20,269 --> 00:04:23,840 So suppose I have one, two, two, three and one. 88 00:04:24,020 --> 00:04:25,520 Let us prepare our hash map. 89 00:04:25,760 --> 00:04:30,920 OK, so I have this element when so when it's count this one. 90 00:04:31,040 --> 00:04:35,400 OK, now element to its count this one again elemental. 91 00:04:35,540 --> 00:04:36,980 So I will increase the count. 92 00:04:37,200 --> 00:04:40,460 OK and I have elementary so push inside the hash map. 93 00:04:41,180 --> 00:04:41,960 Count this one. 94 00:04:42,350 --> 00:04:44,660 Now I have element one to increase the count. 95 00:04:44,780 --> 00:04:47,630 OK, so after doing this our test prep is ready. 96 00:04:48,670 --> 00:04:53,920 After reading over the area, I would have thought I was ready then I would like to over the map and 97 00:04:54,670 --> 00:04:58,690 Guitry has gone to one, so that's why I would answer will be three. 98 00:04:59,350 --> 00:05:05,080 OK, and similarly with this one, also, if you will prepare the hash map, it will be like element 99 00:05:05,080 --> 00:05:06,040 to count one. 100 00:05:06,990 --> 00:05:13,140 Then again, element also I will make the count to then I have element one to element one and count 101 00:05:13,140 --> 00:05:13,560 this one. 102 00:05:13,860 --> 00:05:15,150 So this is the Hashmat. 103 00:05:15,510 --> 00:05:19,980 Now, I trade over the map and you will be able to find the answer, which is one. 104 00:05:20,820 --> 00:05:24,150 OK, so in this case, what is the time and the space complexity? 105 00:05:25,660 --> 00:05:32,440 So basically, time complexities big off, and so we are assuming that the unordered so we lose, we 106 00:05:32,440 --> 00:05:33,730 will use in order to map. 107 00:05:35,550 --> 00:05:42,170 And in order to map the time complexities, basically order of an OK, you can insert, you can search. 108 00:05:42,660 --> 00:05:45,530 So all this time complexities are basically ordered offline. 109 00:05:45,750 --> 00:05:51,850 OK, so the time complexity is just started off and we are trading over the edge and preparing the hash 110 00:05:51,900 --> 00:05:54,820 map and the space complexities basically all that often. 111 00:05:54,950 --> 00:05:59,980 OK, so this is the time and the space complexity for using the hash map. 112 00:06:00,850 --> 00:06:06,290 OK, so you can see we are optimizing on time, but here we are using extra space. 113 00:06:07,170 --> 00:06:09,630 Here we are using extra space to prepare the map. 114 00:06:10,320 --> 00:06:14,370 OK, so now let us discuss the best approach for solving this question. 115 00:06:14,790 --> 00:06:18,240 OK, so let's discuss approach number three. 116 00:06:19,530 --> 00:06:21,390 So we have our approach. 117 00:06:22,470 --> 00:06:24,300 So excited, overrated. 118 00:06:24,330 --> 00:06:25,680 OK, so this is the symbol. 119 00:06:27,060 --> 00:06:34,920 So what is also basically the timetable of SARS, so you have two values, and B, so if this is one 120 00:06:35,670 --> 00:06:40,350 one one zero zero one and zero zero. 121 00:06:41,650 --> 00:06:43,330 And this is basically the output. 122 00:06:43,990 --> 00:06:51,940 OK, so what they are doing, why is basically A, X or B, OK, so when you will apply the operator, 123 00:06:51,970 --> 00:06:52,630 what will happen? 124 00:06:52,870 --> 00:06:56,350 So if the elements are same, then the output will be zero. 125 00:06:56,620 --> 00:07:00,270 So in this case, the elements are same, the output is zero. 126 00:07:00,430 --> 00:07:02,860 And if the elements are different and output is one. 127 00:07:04,480 --> 00:07:06,550 OK, so this is what, Exodus? 128 00:07:06,820 --> 00:07:11,920 OK, so suppose is basically two and basically three, so what will happen? 129 00:07:11,950 --> 00:07:13,450 So this is a bitwise operator. 130 00:07:14,600 --> 00:07:20,660 So how bitwise operators will work, so it will convert to into its binary form, so which is basically 131 00:07:21,110 --> 00:07:26,240 one in zero, will convert three into its binary form, which is nothing but one and one, and then 132 00:07:26,240 --> 00:07:29,100 we will apply the exact operator. 133 00:07:29,390 --> 00:07:31,330 So the elements are different. 134 00:07:31,340 --> 00:07:33,270 Output is one element, the same. 135 00:07:33,310 --> 00:07:33,950 So the output. 136 00:07:34,550 --> 00:07:38,440 And now we will convert this binary into its decimal form. 137 00:07:38,600 --> 00:07:39,830 So it will become one. 138 00:07:39,890 --> 00:07:43,790 OK, so basically two zah two exactly. 139 00:07:43,830 --> 00:07:44,990 Is nothing but one. 140 00:07:45,020 --> 00:07:46,380 OK, so this is how it works. 141 00:07:46,850 --> 00:07:49,010 Now there are some properties of XOL. 142 00:07:49,830 --> 00:07:51,680 OK, so what are those properties. 143 00:07:52,340 --> 00:07:55,440 So in order to solve this question you must know those properties. 144 00:07:55,460 --> 00:07:57,980 So the first properties supposed to have an element A. 145 00:07:59,030 --> 00:08:06,290 So the first properties, if you will, or two same elements, you will get zero second property, so 146 00:08:06,500 --> 00:08:12,500 you have an element and if you will take the order with zero, then you will get itself. 147 00:08:13,080 --> 00:08:14,840 OK, so this is the first property. 148 00:08:14,870 --> 00:08:15,890 This is the second property. 149 00:08:16,220 --> 00:08:21,510 So third property is basically the XOR operator is basically associative. 150 00:08:21,560 --> 00:08:30,830 OK, so if you will write like this A, X or B, X or A, then basically and this, and this will be 151 00:08:30,830 --> 00:08:31,230 zero. 152 00:08:31,280 --> 00:08:34,169 OK, you can see if the elements are same, they are zero. 153 00:08:34,460 --> 00:08:39,250 So what you can write so e x or B x ray. 154 00:08:39,289 --> 00:08:42,799 This is nothing but a X or A, X or B. 155 00:08:43,210 --> 00:08:45,310 OK, and this will become zero. 156 00:08:45,590 --> 00:08:51,590 So I have zero X or B and then we will use the second property. 157 00:08:51,620 --> 00:08:52,910 So this is nothing but B. 158 00:08:53,880 --> 00:09:01,920 OK, so you can write A, X or B, X or so, and they will cancel out each other, you will have B, 159 00:09:02,610 --> 00:09:05,500 OK, so these are the three properties that you should know. 160 00:09:05,710 --> 00:09:09,900 OK, so these are the first two properties and this one is the third property. 161 00:09:11,490 --> 00:09:15,450 Now, let's see how we can use this extra body to solve our question. 162 00:09:15,630 --> 00:09:18,360 OK, so let us take some examples. 163 00:09:19,480 --> 00:09:25,140 So for one, two, one, two, OK, so I have this idea for one, two, one, two. 164 00:09:25,240 --> 00:09:27,310 So this is for this is one. 165 00:09:27,340 --> 00:09:27,970 This is two. 166 00:09:28,000 --> 00:09:28,660 This is one. 167 00:09:28,660 --> 00:09:29,440 And this is two. 168 00:09:29,650 --> 00:09:37,550 OK, what we will do so we will take the XOR off all the elements, take X out of all the elements. 169 00:09:37,570 --> 00:09:45,130 So what it will look like for X or one X or two X or one expert to. 170 00:09:45,520 --> 00:09:47,020 OK, so what will happen. 171 00:09:47,410 --> 00:09:52,630 This one and this one, they will cancel out each other, these two and this, they will cancel out 172 00:09:52,630 --> 00:09:52,960 each other. 173 00:09:52,960 --> 00:09:53,200 Why. 174 00:09:53,200 --> 00:09:54,220 They will cancel out each other. 175 00:09:54,220 --> 00:10:01,150 Because I know e if the elements are sim and I'll take the third output will be zero and if you will 176 00:10:01,150 --> 00:10:05,590 take the Zord with zero you will take you will get itself. 177 00:10:05,620 --> 00:10:07,300 OK, so what will happen. 178 00:10:07,300 --> 00:10:09,110 Only four will be the remaining element. 179 00:10:09,130 --> 00:10:10,510 So our answer will be for. 180 00:10:11,700 --> 00:10:15,180 OK, let us consider one more example, so. 181 00:10:16,080 --> 00:10:20,430 I have one, then I have two, I have two and three and one. 182 00:10:21,580 --> 00:10:25,000 Now, let's take XOR of all the elements of the. 183 00:10:25,420 --> 00:10:31,900 So what it will look like when X to X to excite three Akhavan. 184 00:10:32,500 --> 00:10:36,440 OK, so this two and these two will cancel out each other. 185 00:10:36,700 --> 00:10:38,470 This one will cancel out this one. 186 00:10:38,620 --> 00:10:40,000 So our output will be three. 187 00:10:41,620 --> 00:10:42,890 OK, so very simple. 188 00:10:43,810 --> 00:10:49,510 So what did be the time this was complexity of using this approach, so space complexity will be a rough 189 00:10:49,520 --> 00:10:49,780 one. 190 00:10:50,380 --> 00:10:53,470 OK, and the time complexity will be what we are doing. 191 00:10:53,470 --> 00:10:54,930 We are just outrating over the. 192 00:10:55,510 --> 00:10:57,820 So the time complexity will be big of an. 193 00:10:57,880 --> 00:11:00,130 OK, so this is the best approach. 194 00:11:01,180 --> 00:11:03,250 OK, this is the best approach. 195 00:11:03,640 --> 00:11:06,790 So what we will do, we will take a variable. 196 00:11:06,790 --> 00:11:11,940 Initially that variable will be zero and then we will take the zoref old element. 197 00:11:12,250 --> 00:11:13,870 OK, this has to be zero. 198 00:11:13,900 --> 00:11:16,660 OK, this has to be zero because what will what do you will write. 199 00:11:16,930 --> 00:11:18,240 You will write like something. 200 00:11:18,520 --> 00:11:24,480 So answer equals answer x or let's say the name of that is a aof. 201 00:11:25,620 --> 00:11:32,340 OK, so basically when the value of zero, that is the first element, so that's why you have to initialize 202 00:11:32,340 --> 00:11:40,740 with the zero because you can see this is the property zero X or any element is basically that element 203 00:11:40,740 --> 00:11:41,050 only. 204 00:11:41,100 --> 00:11:43,960 OK, so you have to initialize your answer with zero. 205 00:11:44,340 --> 00:11:45,730 This is compulsory. 206 00:11:45,790 --> 00:11:46,170 OK. 207 00:11:47,340 --> 00:11:48,570 Now that that's right, the good. 208 00:11:50,940 --> 00:11:53,210 So basically, this is a legitimate problem. 209 00:11:53,250 --> 00:11:53,580 OK. 210 00:11:55,030 --> 00:11:56,890 So now let us write the code. 211 00:11:56,920 --> 00:12:03,560 OK, so let's change the name of that to be a so we have to return that integer that is appearing only 212 00:12:03,640 --> 00:12:04,210 one time. 213 00:12:05,820 --> 00:12:08,270 So first, let us create a variable answer. 214 00:12:09,530 --> 00:12:13,430 Which will contain that unique element and this has to be initialized with zero. 215 00:12:14,210 --> 00:12:18,230 OK, so now what we have to do, we are just iterating over the Eddie. 216 00:12:19,240 --> 00:12:20,590 So Edward says. 217 00:12:24,290 --> 00:12:31,190 Now you just have to take the XOR of all the elements, OK, you just have to take the eggs out of all 218 00:12:31,190 --> 00:12:31,700 the elements. 219 00:12:34,950 --> 00:12:37,620 And finally, what you look, you will return, I would answer. 220 00:12:39,010 --> 00:12:40,430 OK, so what I'm doing here. 221 00:12:42,090 --> 00:12:44,890 So let us consider this example for one, two, one, two. 222 00:12:44,940 --> 00:12:47,220 OK, so I have four. 223 00:12:47,640 --> 00:12:51,450 One, two, one, and then two. 224 00:12:52,990 --> 00:12:53,830 So this is one. 225 00:12:53,870 --> 00:12:56,710 OK, so my answer is initially zero. 226 00:12:58,250 --> 00:13:03,680 OK, our answer is initially zero, then I will take the action with the first element so it will become 227 00:13:04,250 --> 00:13:05,980 zero or four. 228 00:13:06,290 --> 00:13:13,400 So this is basically for then I will take the I will take the Tzavela one element. 229 00:13:13,550 --> 00:13:16,170 So this is for X or one. 230 00:13:16,220 --> 00:13:21,380 So this is basically for X or Y and only then I will take the Zoref two. 231 00:13:21,980 --> 00:13:26,630 So this is for X or one and then two. 232 00:13:26,630 --> 00:13:28,550 OK, so this is the value of one. 233 00:13:28,560 --> 00:13:33,010 So you can see answer operator error element. 234 00:13:33,020 --> 00:13:34,400 So this is the answer. 235 00:13:35,890 --> 00:13:38,090 This is the operator and this is the element. 236 00:13:38,430 --> 00:13:42,270 OK, so then I will take XOR with one. 237 00:13:42,680 --> 00:13:44,200 So what is the answer? 238 00:13:44,220 --> 00:13:46,630 The answer is for actual one X or two. 239 00:13:47,230 --> 00:13:54,490 OK, so this is the answer, then the operator and then the area element, which is when now what will 240 00:13:54,490 --> 00:13:54,820 happen. 241 00:13:55,180 --> 00:13:57,730 This one and this one, they will cancel out each other. 242 00:13:58,060 --> 00:14:03,670 So this is basically for X or two, OK, and then this element to. 243 00:14:04,730 --> 00:14:10,360 So our answer will be, which is the current answer to our current answer is, for example. 244 00:14:11,540 --> 00:14:18,530 Then the operator, Exide operator, and then the element of the too, so this two and these two, they 245 00:14:18,530 --> 00:14:19,590 will cancel out each other. 246 00:14:19,970 --> 00:14:22,950 So basically when we will come out, so I will reach here. 247 00:14:23,330 --> 00:14:28,040 So basically, we will come out of this for loop surveillance that will contain food, which is the 248 00:14:28,100 --> 00:14:31,270 right answer, and we will return that answer. 249 00:14:31,320 --> 00:14:32,700 OK, very simple. 250 00:14:33,530 --> 00:14:35,090 Now let us submit our code. 251 00:14:35,340 --> 00:14:35,660 OK. 252 00:14:40,250 --> 00:14:45,330 OK, so our goal is working fine, so let us summarize, so basically. 253 00:14:45,560 --> 00:14:48,830 So basically we have discussed three approaches for solving this question. 254 00:14:49,130 --> 00:14:52,250 First approach was basically desalting approach. 255 00:14:53,630 --> 00:14:56,060 That time, complexity was basically and Logan. 256 00:14:57,520 --> 00:14:59,920 And this space complexity was hard enough when. 257 00:15:01,130 --> 00:15:04,880 The second approach that we discussed, it involves the use of Hashmat. 258 00:15:06,250 --> 00:15:12,750 OK, the time complexity we discussed was big off and and the space complexity was also a big off. 259 00:15:12,760 --> 00:15:17,960 And so we can see here we are improving all the time, but we are taking more space. 260 00:15:18,430 --> 00:15:24,680 OK, now, the third way that the third way to solve this question is basically with the help of our 261 00:15:24,730 --> 00:15:25,210 operator. 262 00:15:25,470 --> 00:15:32,560 OK, the time complexity is basically big off and and the space complexity is basically big of one. 263 00:15:32,590 --> 00:15:35,980 OK, so this is the best approach for solving this question. 264 00:15:36,860 --> 00:15:40,300 OK, and what are the properties of X or so any element. 265 00:15:41,330 --> 00:15:43,910 Exide with the same element, output will be zero. 266 00:15:45,680 --> 00:15:51,950 Any element Exurbia zero basically will be that element only, and the third basically is the associative 267 00:15:51,950 --> 00:15:52,490 property. 268 00:15:53,480 --> 00:15:57,370 So A, X or B, X or A is basically nothing. 269 00:15:57,620 --> 00:16:01,600 It is same as a X or a Asabi. 270 00:16:02,420 --> 00:16:04,690 So this will become zero. 271 00:16:05,030 --> 00:16:14,330 So I have zero X or B and this is nothing but B, OK, so in short you can ride this and disable, cancel 272 00:16:14,330 --> 00:16:14,930 out each other. 273 00:16:14,960 --> 00:16:15,970 Your output will be. 274 00:16:17,290 --> 00:16:19,660 OK, so I hope you understood this question. 275 00:16:21,460 --> 00:16:21,940 Thank you.