Remove duplicates from array in Javascript | Algorithm Interview Question

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

45941021f354d

👍︎︎ 6 👤︎︎ u/1-800-BICYCLE 📅︎︎ Aug 05 2018 🗫︎ replies

Can this be done with .filter?

👍︎︎ 1 👤︎︎ u/pentakiller19 📅︎︎ Aug 06 2018 🗫︎ replies

Is there a way to use javaScript reduce function to do this?

👍︎︎ 1 👤︎︎ u/didiben 📅︎︎ Aug 05 2018 🗫︎ replies
Captions
hey guys welcome to intravenous so today we're gonna try to solve this problem how to remove duplicate values from an array that's an interview question it's a level 1 into your questions interviewer would ask you to gauge how much do you know you know the basics of the array and that's why they would ask you the question before asking tough questions now first thing I would say that there are multiple answer to this mode just like most algorithm there is not a one solution and we're gonna look at more like a brute force approach then using a different data structure and then a JavaScript trick to write in a single line of code so let's get started I've asked this question many times in an interview and few of those people they have just started coding without even understanding and then they end up writing how to find duplicate values inside the area rather than how to remove duplicate values from the array so always pay attention to what your interviewer says try to try to then confirm with them so for example I would do something like this so I would say let's see if I have an array like this okay and it has one two five two one one and eight or something like this right now do you mean that when I remove the duplicates I would have one two five and eight okay do I need to preserve the order in this here can I just reorder things you know things like that and that would give the interviewer confidence in you that this guy actually listens and interacts before starting his or her work so that's the main thing so let's start with a brief brute force algorithm brute force algorithm would be simplest way to do it one way to do it is have a separate array so I can have a let be and what I can do I can go to each item in the array and then see if I have revisited this before and how can I find out if I visited this before as soon as I visit one so let's say if I visit one I would push it into array B then I was at 2 then I would push it that into an array B then I was at 5 then I would push 5 into array then I would revisit 2 now again I check inside B if if it's there then I'm not gonna push it again I'm gonna check one it's not there it's still there and so I'm not gonna push it and then I'll have 8 and I'll crush you 8 and so I have my I don't really have to remove it from here I can create a new array which would have only unique value start with a for loop so I can say let I equal to 0 make sure that you do let a inside if I just do I then it creates a global variable which we don't want and I could say I is less than a dot le and GTH Lent again you can do the Lent here but I would recommend doing it here that is because if I do it like this on every iteration it recalculates a length which is more expensive and since you are trying to explain the algorithm you want to do the best algorithm possible so you need to explain this to say I can say let len equal to e dot length okay and then I can just use Lent here so that would be a little faster and I plus plus if B dot index of a I which means if we find a I in to remember I said we were gonna check B to see if it's there before pushing it so that's what we are doing here if this is equal to minus one usually when you call index off if you can't find it it returns minus 1 so that's what it is and if it's not there then simply push into it push a I and that's all we need to do because let's say if you find it then you don't have to do anything so in the end we would have all the values that you need it all the unique values so if I do console.log B this should have all the values if I run this I would get 1 2 5 and 8 so it works now what's the complexity of this algorithm because for the length of the array for each element we are trying to go to the entire length again because we are checking inside B to see if if it's there right so it's n square which is or close to Instagram right which is kind of highest so now let's look at the solution where you have a little lower so one thing we can do if your interviewer doesn't care about the order of the array so I have this is not in order so if I simply sort it and let's say if I remove all this stuff I need lengths so I'm just gonna remove here so what I can do is I can say a dot sort and that would make the array something like this so 1 1 2 2 5 & 8 alright now here I don't need to have an extra already to track things I can have a simple variable which tracks if I have a previously encountered this because since there there were right and so let's do it here so I still have to write a for loop you can use a while loop as well and here I can have a we'll call temple so I can tell that underscore tap ok and here what I will do is I will check if if I have encountered this value inside temple so I can say if a i underscore temp sorry if it's not equal to so if I haven't encountered before then I would make temp Dai and I also need to be dot push a I so if it's never there that I'm just gonna push it but I'm not comparing each value would be again I'm just pushing it here right and so if I around this I would get the same result okay so the complexity is here if you know if you're using quicksort then the complexity or the kids quicksort will be login and then you have to go through each element so like + n so it's much less than N squared complexity now if I using Java I would use a hash table but since JavaScript doesn't provide hash tables I can use such a JavaScript object to do this so I would just create a JavaScript object which is an empty object and then I can use for off loop so I can say for let I of a if you don't know what for off loop is I have a tutorial on iterators I can provide a link here that would help you but it's much better than for off for each or for regular for loop so I would be the value not the index here and so all I need to do now is I can say object equal to true now what does it really mean so this is an object right so it has a key add value so I'm setting key to the value of the array and and the value of this object should be always true so if I simply print this out let's say object you would see what is going on here alright so you can see something like this because the key has to be unique it won't store anything that is duplicate so it's automatically storing only key that are unique so I already have kind of a unique but it's not an array it's an object right and the value is true so how do I get the array back from here so there is a neat way to do it so I can say object dot keys and I can pass the object and that should give me all the keys from here and I can call it let be equal to this and if I now print B I should see all the unique values so here I see one two five and eight so this is pretty nice and it's actually much faster the complexity of this I believe is n all right last but not least I want to do this in one line so how do I do this so JavaScript es2016 has introduced a new data type in JavaScript call set and the way it works is I can say let be set equal to new I have to use a constructor and I can say set now what is the speciality of set set only stores unique values by default so if I pass this array a it should only store unique values so I would get a set with only unique values so if I console.log be set I should get the unique values but I'm getting a set I'm not getting an array back so you can see it has entry and stuff so how do I get the erase back so let's remove this and if you know the spread operator so if I simply put square bracket around it and put dot dot dot then this should convert the set into an array if you don't know what the set square operator is and how to use it properly I have a tutorial on it I'll provide a link here and all I have to do it just console.log this so let's look at it that should give me an F our way back so it does so this is a one line solution I think this is the best solution and also previous solution also fine so you can pick and choose so I hope you learned something from this tutorial if you did please like subscribe and provide a constructive comment thank you and don't forget to subscribe the channel
Info
Channel: InterviewNest
Views: 179,326
Rating: undefined out of 5
Keywords: JavaScript, Algorithms in JavaScript, JavaScript Interview Questions, Algorithm Interview Questions, Interviewnest, interviewnest.com, programming interview
Id: dvPybpgk5Y4
Channel Id: undefined
Length: 12min 7sec (727 seconds)
Published: Thu Feb 08 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.