Skip to content

Instantly share code, notes, and snippets.

View truonglocbinh's full-sized avatar

truong.loc.binh truonglocbinh

View GitHub Profile
@truonglocbinh
truonglocbinh / gist:29d059b23dfd4a6a69186fe3450abb2c
Created January 25, 2018 08:11 — forked from lsauer/gist:1305056
Easiest way to find duplicate values in a JavaScript array - Native unique function implementation
//lo sauer, 2011; lsauer.com
/**
* I saw this thread: http://stackoverflow.com/questions/840781/easiest-way-to-find-duplicate-values-in-a-javascript-array
* The solutions above lacked the elegance that can be done a with map-reduce-like operations
* Since this implementation works with native functions, the speed is in most circumstances faster
* than any solution using scripted-logic
* Additionally, I needed to quickly filter duplicate url-entries for: http://lsauer.github.com/chrome-session-restore/
*/
//copy and paste: without error handling
Array.prototype.unique = function(){return this.sort().filter( function(v,i,o){if(i>=0 && v!==o[i-1]) return v;});}