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 responses to “Remove duplicates from array in Javascript | Algorithm Interview Question”

  1. 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);

  2. 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.

  3. 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.

Leave a Reply