Created
September 5, 2019 03:57
-
-
Save rakin92/eb1c2d22adbc2ea212069df766821319 to your computer and use it in GitHub Desktop.
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
// 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