Several methods 2019.08.30 array and the time required to re Comparative

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数组去重方法</title>
</head>
<body>
<script>


the unique1 function (ARR) {
// iterate ARR
for (var I = 0, R & lt = []; I <arr.length; I ++) {
// iterate R & lt
for (var J = 0; J <r.length; ++ J) {
// if the element r is equal to the current traversing the element of arr loop exits
IF (r [J] == arr [I]) {
BREAK;
}
}
// if not found last traversed with the current arr elements of the same element
IF (J == r.length) {
// put this element appended to the
R & lt [r.length] = ARR [I];
}
}
return R & lt;
}

 


the unique2 function (ARR) {
// iterate ARR
for (var I = 0, the hash = [], R & lt = []; I <arr.length; I ++) {
// If the hash does not exist in the array arr [i ] of the underlying variable
IF (the hash [ARR [I]] === undefined) {
// this value into the final array
R & lt [r.length] = ARR [I];
// and this value as the next hash table array storage, a custom value to
the hash [ARR [I]] =. 1;
}
}
return R & lt;
}

 


unique3 function (ARR) {
// the algorithm consistent with the second embodiment, because the underlying object storage is an array
var obj = {};
// iterate
for (var i = 0, r = []; i <arr.length; I ++) {
// if obj is not present in the current attribute value of the attribute name arr
! IF (obj [arr [I]]) {
// this value into the final array
r. Push (ARR [I]);
// and this value as the hash table array storage, a self-defined value to the
obj [ARR [I]] =. 1;
}
}
return R & lt;
}

 


/ * function unique4 (ARR) {
// sort to ARR
arr.sort (function (A, B) {return ab &})
// iterate
for (var. 1 = I; I <arr.length; I ++) {
// If the current value that is equal to the preceding value
IF (ARR [I] === ARR [-I. 1]) {
// delete the value
arr.splice (I,. 1);
// because the deletion of a value need to a loop is moved forward
i--;
}
}
return ARR;
} * /

 


/ * function unique5 (arr) {
// iterate
for (var I = 0; I <arr.length; I ++) {
// iterate to find the current value equal arr
for (var j = i + 1 ;; ) {
subscript j // found record
j = arr.indexOf (ARR [I], j);
// if j is -1, and the representative value is not followed by the same
IF (j == -1) {
BREAK ;
}
// delete the value
arr.splice (J,. 1);
}
}
return ARR;
} * /

 


unique6 function (ARR) {
// new syntax for ES6
return the Set new new (ARR);
}

ARR = var [5,4,9,1,6,8,7,5,4,2,4,5,6,4,2,6,4,2,1,5,9]
// test
console .log (the unique1 (ARR));
the console.log (the unique2 (ARR));
the console.log (unique3 (ARR));
// the console.log (unique4 (ARR));
// the console.log (unique5 (ARR ));
the console.log (unique6 (ARR));
// efficiency test
for (var I = 0, arrtest = []; I <10000; I ++) {
// random array of which added to the data 10000
arrtest [i] Math.ceil = (Math.random () * 100)
}


console.time("unique1");
unique1(arrtest);
console.timeEnd("unique1");
console.time("unique2");
unique2(arrtest);
console.timeEnd("unique2");
console.time("unique3");
unique3(arrtest);
console.timeEnd("unique3");
/*console.time("unique4");
unique4(arrtest);
console.timeEnd("unique4");*/
/*console.time("unique5");
unique5(arrtest);
console.timeEnd("unique5");*/
console.time("unique6");
unique6(arrtest);
console.timeEnd("unique6");
</script>
</body>
</html>

Guess you like

Origin www.cnblogs.com/awei313558147/p/11537194.html