题目:去掉数组[4,3,"3",3,5,7,5]中的重复元素,返回[4,3,"3",5,7]
(function() {
'use strict';
function filter1(arr) {
var b = [];
arr.forEach(function(i) {
if (b.indexOf(i) == -1) {
b.push(i);
}
});
return b;
}
function filter2(arr) {
var b = {}, c = [];
arr.forEach(function(i) {
b[i] = b[i] ? b[i] : {};
var type = typeof i;
if (!b[i][type]) {
b[i][type] = true;
c.push(i);
}
});
return c;
}
function timer(fn, arr) {
console.time('filter');
fn.call(this, arr);
console.timeEnd('filter');
}
function testArr(n) {
// var arr = [4,3,"3",3,5,7,5];
var arr = [];
for (var i = 0; i < n; i++) {
arr.push(i);
arr.push(i + "");
}
return arr;
}
for (var i = 1; i <= 100; i++) {
console.log(i * 10);
var arr = testArr(i * 10);
timer(filter1, arr);
timer(filter2, arr);
}
})();
下载本文