Faster implementation (Better for short or medium size arrays):
var a = ["AAA", "BBB", "CCC", "AAA", "BBB", "CCC", "AAA", "BBB", "CCC", "DDD"];
// Faster implementation:
// (Better for short or medium size arrays)
var b = a.filter((function(){
var idx = {};
var cont = true; // Continue
return function (item){
if (! cont) return false;
if (idx[item]) return cont = false;
return idx[item] = true;
};
})());
console.log(b);
// [ 'AAA', 'BBB', 'CCC' ]
Implementation with minimal memory waste (but highly unefficient):
// Minimal memory waste implementation:
var b = a.filter((function(){
var cont = true; // Continue
return function (item, i){
if (! cont) return false;
if (a.slice(0, i).filter(x=>x==item).length) return cont = false;
return true;
};
})());
console.log(b);
// [ 'AAA', 'BBB', 'CCC' ]
Of course, there is infinite intermediate approaches (depending on your needings). But, If you donsn't need to parse HUGE arrays, the best solution is to use the first approach: Index will be freed by garbage collector as soon as filter process finishes and, in fact, if you are lucky and repetition happen early, then no much memory will be used ;-)