var arrs = [1, 1, 1, 2, 3, 4, 5, 6, 6, 7];
function baseUniq(array) {
var length = array.length,
result = [],
seen = result;
outer: while (++index < length) {
var value = array[index],
computed = value;
var seenIndex = seen.length;
while (seenIndex--) {
if (seen[seenIndex] === computed) {
continue outer;
}
}
result.push(value);
}
return result;
}
console.log(baseUniq(arrs));
原理是数组共享了一个地址,当result有值的时候,seen也会有之,然后在原数组遍历的时候判断result的要push的值seen里面有没有,有的话直接continue outer;跳出循环。
如有需要可以增加数组是不是空数组的判断等