Created
September 6, 2019 19:00
-
-
Save masautt/df672d72e0fa70e596c5a916944459aa to your computer and use it in GitHub Desktop.
How to find the first duplicate element of an array in JavaScript?
This file contains hidden or 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
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