Last active
September 11, 2015 14:50
-
-
Save victorpolko/d40a2b12dd93e7df90f9 to your computer and use it in GitHub Desktop.
JavaScript: collect duplicates in array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var duplicates = function(array) { | |
var iteratee = array.slice(0); | |
var seen = []; | |
return array.filter(function(element) { | |
iteratee.shift(); | |
if (seen.indexOf(element) !== -1) return false; | |
var hit = iteratee.indexOf(element) !== -1; | |
if (hit && seen.indexOf(element) === -1) seen.push(element); | |
return hit; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Another version based on my uniq.simple Gist
(or)