 What's up guys, my name is Michael and welcome to my YouTube channel today. We're gonna do another lead code grind Basically and repeated elements in size 2n array in an array a of size 2n There are n plus one unique elements exactly one of these elements is repeated n times, okay? So return the element that is repeated n times Okay, so Essentially this question is pretty basic you're given a an array of 2n of size 2n and One of these elements is repeated n times return the element that is repeated n times so in this case one two three three the size is four and The which is 2n So something is repeated twice and that's the Value three three is repeated twice in this case the length is one two three four five six six divided by two is three Some numbers repeated three times and and in this case It's two because two is repeated three times one two three and return to in this case the values one two three four five six seven eight Eight there's eight elements one element is repeated four times Let's see five is repeated four times one two three four Yeah, five is repeated four times. We're returning five right the element five, okay, so You guys try doing this yourself I will come back with they and tell you the solution Basically, it is not that difficult just Yeah, it's not that difficult if you know that a certain data structure definitely help you in this case It is not that difficult But come back when you figure out for an hour and then if you could figure this out and I'll tell you how to do it But yeah All right guys, so I'm gonna do it this with you guys right now first in order to do this You need to know the understanding of data structure of maps What we're gonna do is we're gonna map every single value with the number of occurrences it has in the array So how do we do that? we're gonna go through the array of We're gonna go through every single value in the array and Basically, we're going inside this map We are going to add a at I we're going to add Every occurrence that occurs into there in inside this array So if if a has a value one, it's gonna set whatever Value at one is gonna add it So initially one probably has zero then adds by one. So one's gonna have one two and then the next time this loop goes Two is gonna have the value of one because two original was zero now We map it the number currents plus one is gonna have two three is gonna have one then three's then after when it sees three again Three is gonna have two. All right after you go through occurrences of All the values in this array Basically, we want to go through our map. So how do we do that? There's a there's a way to do this really easily Go through values of map in C++ Let's see As much as there's a better way to do it and there was a not good a way to do it There's an easy way and there's a hard way. I'd rather go do the easy way because easy way is much faster Oh this one. Yeah Yeah for autoconst every key value pair. So I'm gonna copy this Okay, so for every Get the key value pair. So for every Autoconst for every key value inside occurrence Basically, I'm gonna go through all the key value pairs and I'm gonna get the ones that has the value n That is repeated n times and I'll return whatever Whatever value that is So in order to get that I need to take So a is a is a a has a size of 2n I need to get The value n so I'm gonna do n is gonna equal to a divide by 2 and that's gonna give me n Now I'm gonna go through my occurrences and then I'm gonna get my You get my I Gotta check the value The element that has occurred repeated n times so if Value is equal to n So values occurrences because I'm having each key value then I'm gonna return What am I to return? I'm gonna return Key. Yeah Values equal then I'm gonna return three key else return negative one and this should work I Ideally this should work. Oh a dot size Okay a dot size Yep Accepted. Yeah, okay, so that's basically It's only faster than 9% Yeah, this is how you would do it. I don't know how else you would do it. Let's see other people solution Oh wait, there's a solution Okay, yeah, that's what I did compare Okay, we are going to I'm gonna I'm gonna look at this and I try to explain their solution of it This is seems way more complicated But I'll try to explain it Okay, so their unique solution is actually very very hacky when I say hacky it only works in this specific problem It wouldn't work for other problems so Yeah If you think of it in this case if you're given an array of two n elements, right if and one at least one element is repeated End times that means Half of the array is one is going to be repeated, right? Half of the array is going to be repeated If half of the array is repeated no matter how much we shuffle it For one of the repeated elements there has to be another repeated element at least three positions away Okay Otherwise Their way the array isn't to end. Do you get what I'm saying? So if you if you look at this case If you look how that they they explain it here if at least one of the Major elements has the same value and As a neighbor within a radius of three then all of the n major elements neighbors have a Distance of at least four then we need an array whose length is at least Four n minus four to contain these n elements therefore by contradiction This would work Okay, so they're saying is as that because The half of the array is repeated in order for this to work there's going to be at least one repeated element and Three positions away from what we're currently looking at. Okay, so This is really hacky basically if I start at I'm going to go through three positions away. I'm going to go through Three positions away from whatever I'm looking at and I'm going to Go through all elements of the array right and I'm going to go through every three positions not every three positions. I'm going to go through I'm Basically going to go through these these positions of the array and I'm going to check like a radius of three. That's what it means. Okay That's what this bounds is for. Okay then if If at least one of them is repeated in these three positions then it must be The value that is repeated end times This is so hacky. It wouldn't work in other situations though, but based on this problem it would work Yeah, and it works. This is the solution for and repeated elements in Size 2n array Ray com subscribe. I Hope you guys understand what the idea is But sometimes I don't I'm not actually not a hundred percent sure why this is what it is But basically it works like that yeah