Skip to content

Instantly share code, notes, and snippets.

@rakin92
Created September 5, 2019 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rakin92/eb1c2d22adbc2ea212069df766821319 to your computer and use it in GitHub Desktop.
Save rakin92/eb1c2d22adbc2ea212069df766821319 to your computer and use it in GitHub Desktop.
// Using Set
function uniqArray(a) {
return [...new Set(a)];
}
console.log(uniqArray([]))
console.log(uniqArray([1, 2, 3, 4]))
console.log(uniqArray([1, 2, 1, 3, 2, 4]));
console.log(uniqArray([1, 1, 1]));
// Interating over array using Memo
function uniqArray(a) {
var seen = {};
return a.filter((item) => {
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
});
}
console.log(uniqArray([]))
console.log(uniqArray([1, 2, 3, 4]))
console.log(uniqArray([1, 2, 1, 3, 2, 4]));
console.log(uniqArray([1, 1, 1]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment