Coding Interview Week 1 | Array And Strings | Intersection Of Two Arrays

Hello this is soon this is my third video for interview with one about one of our a questions this question is from the code called intersection up to erase there are two examples such as if there is are two arrays one ticktick one contains in numbers 1 and T 2 then we have intersection up to two and four.
Nine five nine four 984 then we have intersection of.

Nine and four here we don’t have eight or four or another nine and nums one so if we use a breath words that’s going to take n times n which is n e stands for the size of gnomes one and.
N m is when M is size for number two also even.

Simple breath works doesn’t work because we need to keep track.

Of which element was already used to keep track of that we need to use like some data structure.

And then insert that value or the index of each one um and then we need to ask like did you use it already so instead of doing that logic I’m probably going to use the sorting algorithm first so that’s gonna take like let’s say if num so one size is M and then non-street sizes M then I’m going to search them first let’s say I’m.

Instead of that I’m going to use one to one so M size M here is 3 and M if he used our Java’s array sort function that’s gonna take n log N and M Logan M so searching will take twice which you we’ll be n log n 2 plus and Logan after we served we’re gonna get the array like this from here I’m going to use two.

Pointers so here we know that when they’re these are same I’m going to insert one of the volume you.

Can insert either from here or there and then move both pointers again you met I that’s let’s make a little more complicated array 3 yeah so here I’m going to have 3 here the reason is that.

I want to show what how both um pointer is moving for the case.

Of the elements are same you’re removing both pointers at the same time and then you are let’s say this.

One as 1 yep in this case since p1 is pointing to the one on the elements that’s smaller than p2 you are moving a view one only and here p2 is pointing I mean one pin 1 is pointing the same elements as p2 we’re gonna insert this value and here we’re gonna move both pointers p1 is out of bounds so we’re going to escape the loop otherwise if let’s.

Say if there was a another element at the end which was to 2 then we’re gonna move the p1 here um here which is in the exits war and then compared with on p2 and p1 and since.

P1 is less than p2 we’re gonna move Pete one only something like this if I’m comparing both elements if those are same then you’re.

Moving both at the same same time and also before moving that or you’re gonna insert into the list and then move if one is less then you use that pointer more right because we know that it’s.

Know that another greater or equal elements it’s waiting for us if you move the pointer one step to the right so in this example we found the intersection of 1 1 & 2 even though there’s another one that’s same as other other duplicates we skip that because we are moving the pointer at the same time whenever we met up the same elements but so we are not going.

To count them anymore so we find the exact same intersection however since we are adding to a some place that we don’t know the size I’m gonna.