Toolroom Tech Blog

Devlopers Digest

JS Snippet 1: Remove duplicate values from an array

How to remove duplicate values from an Array in JavaScript

See a working example here: http://jsfiddle.net/liboswar/4bq805oL/

function unique(arr) {
    var i,
      len=arr.length,
      out=[],
      obj={};
   
    //Store each value as key/value pair in a new object
    for (i=0;i<len;i++) {
        obj[arr[i]]=arr[i];
    }
   
    //Now push the object into the result array
    for (i in obj) {
        out.push(obj[i]);
    }
    return out;
}

console.log(multiples([1,2,3,4,4,5,5,'Hello World','Hello World',6,7,7,7,7,7,8], parseInt));
//result: [1, 2, 3, 4, 5, 6, 7, 8, "Hello World"]

Loading