Skip to content

Instantly share code, notes, and snippets.

@masautt
Created September 6, 2019 19:00
Show Gist options
  • Save masautt/df672d72e0fa70e596c5a916944459aa to your computer and use it in GitHub Desktop.
Save masautt/df672d72e0fa70e596c5a916944459aa to your computer and use it in GitHub Desktop.
How to find the first duplicate element of an array in JavaScript?
function firstDupe(arr) {
return arr.findIndex((item, index) => arr.lastIndexOf(item) !== index)
}
console.log( firstDupe([8,6,7,5,3,0,9])); // --> -1 = !exist
console.log( firstDupe(["L", "E", "T", "T", "E", "R"])); // --> 1 = E
// https://stackoverflow.com/questions/39346182/javascript-how-to-find-first-duplicate-value-and-return-its-index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment