JavaScript Snippet - Two Approaches On Unique'fing An Array - Memory Wise

Posted at

Since nigher Unique'fing nor uniqify are actually words in the English language [should they be though?] but taken from a developer's terminology jargon.. I'll choose the later.. :]]

#1
function uniqify(array){ /* note: depending on memory-efficient implementations, this can be huge memory waste.. but it is sort-of one-liner... :] */
return Object.keys(
array.reduce(function(carry, item, index){
carry[ item ] = true;
return carry;
}, {})
);
}


#2
function uniqify(array){ /* note: using JavaScript's OO module, tmp variable does no-longer requires dirty dirty "var" lines */
array.tmp = {};
array.forEach(function(item){
array.tmp[ item ] = true;
});
return Object.keys(array.tmp);
}