обычно я делал так ...
новый вариант и сравнение
var time_roni = new Date
for (var a=0; a<10000; a++) {
var array = [1, 2, 1, 10, 5, 3, 4, 40, 50],
tmp = {},
result = [];
for (var i=0; i<array.length; i++) {tmp[array[i]] = tmp[array[i]]?++tmp[array[i]]:1}
for (var i = 0; i < array.length; i++) {
tmp[array[i]] == 1 && result.push(array[i])
}
}
var time_roni = (new Date).getTime() - time_roni.getTime() ;
function uniquesOnly ( source ) {
var uniqueness = new Map();
source.forEach( function ( value ) {
switch ( uniqueness.get( value )) {
case undefined: uniqueness.set( value, true ); return;
case true: uniqueness.set( value, false );
}
});
return source.filter( function ( value ) {
return uniqueness.get( value );
});
}
var time_dzen = new Date
for (var a=0; a<10000; a++) {
uniquesOnly([1, 2, 1, 10, 5, 3, 4, 40, 50]);
}
var time_dzen = (new Date).getTime() - time_dzen.getTime() ;
var time_danik = new Date
for (var a=0; a<10000; a++) {
var array = [1, 2, 1, 10, 5, 3, 4, 40, 50];
var result = [];
for (var i = 0; i < array.length; i++) {
var item = array[i];
if (array.indexOf(item, i + 1) === -1 && (i === 0 || array.lastIndexOf(item, i - 1) === -1))
result.push(item);
}
}
var time_danik = (new Date).getTime() - time_danik.getTime() ;
alert([time_roni,time_dzen,time_danik])