Home  >  Article  >  Backend Development  >  Several methods to remove duplicates from Javascript arrays

Several methods to remove duplicates from Javascript arrays

巴扎黑
巴扎黑Original
2016-11-21 15:15:15767browse

Implementation idea: Create a new array, traverse the incoming array, and add the value if it is not in the new array; Note: The method "indexOf" to determine whether the value is in the array is an ECMAScript5 method, which is not supported by IE8 and below. It requires more Write some code that is compatible with lower version browsers:

//The simplest array deduplication method
function unique1(array){
var n = []; //A new temporary array
//Traverse the current array
for (var i = 0; i < array.length; i++){
//If the i-th item of the current array has been saved into the temporary array, skip it,
//Otherwise, push the current item into the temporary array
if (n.indexOf(array[i]) == -1) n.push(array[i]);
}
return n;
}

Object key-value pair method

Implementation idea: create a new js object As well as a new array, when traversing the incoming array, determine whether the value is the key of the js object. If not, add the key to the object and put it into the new array. Note: When determining whether it is a js object key, "toString()" will be automatically executed on the incoming key. Different keys may be mistaken for the same; for example: a[1], a["1"]. To solve the above problem, you still have to call "indexOf".

// The fastest and takes up the most space (space is exchanged for time)
function unique2(array){
var n = {}, r = [], len = array.length, val, type;
for (var i = 0; i < array.length; i++) {
val = array[i];
type = typeof val;
if (!n[val]) {
n[val] = [type];
r. push(val); Return r ;
}


Array subscript judgment method

Implementation idea: If the i-th item of the current array first appears in a position other than i in the current array, it means that the i-th item is repeated and ignored. Otherwise, store the result array.

function unique3(array){

var n = [array[0]]; //Result array

//Traverse starting from the second item

for(var i = 1; i < array.length; i++) {的 // If the first item of the current array is not i in the current array,

// then it means that item i is repeated and ignores it. Otherwise, store the result array

if (array.indexOf(array[i]) == i) n.push(array[i]);

}
return n;
}


Post-sort adjacent removal method

Implementation idea: Sort the incoming array so that the same values ​​are adjacent after sorting, and then when traversing, only add values ​​that are not duplicates of the previous value to the new array.

//Adjacent the same values, and then traverse to remove duplicate values ​​
function unique4(array){

array.sort();

var re=[array[0]];

for(var i = 1; i < array.length; i++){

if( array[i] !== re[re.length-1])

{

re.push(array[i]);
}
}
return re;
}


Optimizing array traversal method

Implementation idea: Get the rightmost value without repetition and put it into a new array. (When duplicate values ​​are detected, the current loop is terminated and the next round of judgment of the top-level loop is entered)

// Idea: Get the rightmost value without duplication and put it into a new array
function unique5(array){
var r = [];
for(var i = 0, l = array.length; i < l; i++) {
for(var j = i + 1; j < l; j++)
if (array[i] === array[j]) j = ++i;
r.push(array[i] );
}
return r;
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn