Last active
February 13, 2020 15:31
-
-
Save gecugamo/5f7240dd05c5cb577a5bc44aa37e42ee to your computer and use it in GitHub Desktop.
JS Unique with Recursion (es5)
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
function unique(array, newArray, index) { | |
if (index === array.length) return newArray; | |
if (newArray.indexOf(array[index]) === -1) newArray.push(array[index]); | |
index += 1; | |
return unique(array, newArray, index); | |
} | |
// unique([1, 2, 2, 3, 4, 5, 5], [], 0); | |
// [1, 2, 3, 4, 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment