Skip to content

Instantly share code, notes, and snippets.

@gecugamo
Last active February 13, 2020 15:31
Show Gist options
  • Save gecugamo/5f7240dd05c5cb577a5bc44aa37e42ee to your computer and use it in GitHub Desktop.
Save gecugamo/5f7240dd05c5cb577a5bc44aa37e42ee to your computer and use it in GitHub Desktop.
JS Unique with Recursion (es5)
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