• Monday , 16 September 2019

## Remove duplicates from array in Javascript | Algorithm Interview Question

How to Remove Duplicate values from an array of integers. Four solutions, 1 ) One brute-force method using a for loop, 2)sort and remove, 3) using JavaScript Objects and 4) Using ES6 / ES2015 Sets.

Original source

## 21 Comments

1. ##### bijay kurra
February 16, 2019 at 14:55

how _temp works here please anyone know

2. ##### Durga Prasad vanapalli
February 16, 2019 at 14:55

RU Indian?

3. ##### Komal Agrawal
February 16, 2019 at 14:55

In my interview I was asked to sort alphanumeric array [5,8,az,bt,68,gy,15]

4. ##### Mahesh Nandam
February 16, 2019 at 14:55

This approach helps me to find easy way to find min and max, Thanks TechSith

var ar = [1,3,4,4,6,12,6,8,-9,1,3,2,23,7];
let b = […new Set(ar)];
b.sort((a,b)=>a-b);
let min = b[0];
let max = b[b.length-1];
console.log(min + ' '+ max);

5. ##### After TheTech
February 16, 2019 at 14:55

what if you get [ [ 1, 7 ], [ 6, 2 ], [ 7, 1 ], [ 2, 6 ], [ 1, 7 ], [ 4, 4 ], [ 4, 4 ] ] and you want [ [ 1, 7 ], [ 6, 2 ], [ 7, 1 ], [ 2, 6 ], [ 4, 4 ] ]

6. ##### Anirudh Aima
February 16, 2019 at 14:55

G8.. i really needed this solution

7. ##### Margarita B
February 16, 2019 at 14:55

Hello! Can anyone, please explain me how exactly _temp encounters the value, I didn't understand this piece. Thanks!

8. ##### rahul sarath
February 16, 2019 at 14:55

sir please make more interview programming question ,this helps me confident in the interview

9. ##### After TheTech
February 16, 2019 at 14:55

that thing you did at 9:18 is f**** amazing!

10. ##### Purva Sawant
February 16, 2019 at 14:55

Hey, I love your tutorials and the way you explain everything line by line. Liked, Subscribed and already shared your tutorials with my colleagues. Looking forward for your upcoming videos.

Just noticed,
Object.keys method provided all the string values in the array.

11. ##### Smart Programming
February 16, 2019 at 14:55

nicely explained, keep it up sir, thank you 👍👍👍👍🙂🙂🙂🙂

12. ##### Narendra Patil
February 16, 2019 at 14:55

Very nice

13. ##### Krešimir Cindrić
February 16, 2019 at 14:55

In the first algorithm, you have the variable len defined globally, which is unnecessary (the same reason why we put let i = 0). It's better to write the for loop as this:

for(let i = 0, len = a.length; i < len; i++)

This way, a.length is accessed only once, which is good, but len is cleared from memory the moment the loop finishes.

14. ##### Mr♤Sir
February 16, 2019 at 14:55

Freaking amazing

15. ##### mostak ahamed
February 16, 2019 at 14:55

thanks. your video easy to learn.

16. ##### irshad ansari
February 16, 2019 at 14:55

First of all why this fake accent

17. ##### Dzintars Klavins
February 16, 2019 at 14:55

Really neat thing!!! Please do more such a practical puzzle pieces.

18. ##### Prath Prath
February 16, 2019 at 14:55

var array = [1, 1, 2, 3, 4, 4, 5];
var filtered = [];

for (var i = 0; i < array.length; i++) {
if (!filtered.includes(array[i])) {
filtered.push(array[i])
}
}
console.log(filtered)

19. ##### ahmer9800
February 16, 2019 at 14:55

Weird, when I do:
let a = [3, 2, 9, 2, 1, 3, 2, 4, 5];

console.log([…new Set(a)]);

I get a result of: [ 3, 2, 9, 1, 4, 5 ]

it doesn't sort for me unless I put .sort() in the a variable.

20. ##### Joy Sen
February 16, 2019 at 14:55

I love your videos, thank you for your time and effort. Removing a duplicate element from an array is a tricky thing.By using collection(in Javascript it is Set) it can be done easily.

21. ##### deepak ratra
February 16, 2019 at 14:55

this was awesome…