Saturday, November 12, 2011

My Favorite Javascript Method for Removing Array Duplicates

var dedup = function(a)
    var b = [], //create a replacement array
        i=a.sort().length; //get the dups together
    while(i--) //loop down the original array
        // they are bunched, so copy only non matches
            b[b.length] = a[i];
    return b; // return the replacement array
// test it
console.log('Original Array: bob,sally,9,sally,george,george,andrew,23,230,\'23\'\n' + 'Deduped Array:'+ dedup(['bob','sally',9,'sally','george','george','andrew',23,230,'23']));

1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai