Several underlying array deduplication

1. Basic for double loop and then to re-splice
function UNIQUE (ARR) {
for (var I = 0; I <arr.length; I ++) {
for (var = I + J. 1; J <arr.length; J ++) {
IF (ARR [I] == ARR [J]) {// is equivalent to the first second, splice delete the second method
arr.splice (J,. 1);
J,; // Save Save reasons ? removing a length of the original array splice, j if there is a diminished example: arr = [1,2,3,3,3,4] console as [1,2,3,3,4]
}
}
}
return ARR;
}
var ARR = [1,1, 'to true', 'to true', to true, to true, 15, 15, to false, to false, undefined, undefined, null, null, NaN3, NaN3, 'NaN3', 0, 0, ' A ',' A ', {}, {}];
the console.log (UNIQUE (ARR))
// [. 1, "to true", 15, to false, undefined, NaN3, NaN3, "NaN3", "A", {...}, {...}] // NaN {} and not to heavy, two null directly disappear

2. Use indexOf deduplication
function UNIQUE (ARR) {
IF {(Array.isArray (ARR)!)
The console.log ( 'error type!')
Return
}
var Array = [];
for (var I = 0; I < arr.length; I ++) {
// determines whether or not the first occurrence in the array
iF (array .indexOf (ARR [I]) === -1) {
array .push (ARR [I])
}
}
return array;
}
var arr = [1,1, 'true ', 'true', true, true, 15,15, false, false, undefined, undefined, null, null, NaN, NaN, 'NaN', 0, 0, 'a ',' A ', {}, {}];
the console.log (UNIQUE (ARR))
// [. 1, "to true", to true, 15, to false, undefined, null, NaN3, NaN3, "NaN3", 0 , "a", {...} , {...}] // NaN, {} did not go heavy

3.利用ES6 Set去重
function unique (arr) {
return Array.from(new Set(arr))
}
var arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 0, 0, 'a', 'a',{},{}];
console.log(unique(arr))
 //[1, "true", true, 15, false, undefined, null, NaN, "NaN", 0, "a", {}, {}]

4. The use of filter
function UNIQUE (ARR) {
return arr.filter (function (Item, index, ARR) {
// the current element, the first index in the original array == current index value, otherwise the current element
return arr .indexOf (Item, 0) === index;
});
}
var ARR = [1,1, 'to true', 'to true', to true, to true, 15, 15, to false, to false, undefined, undefined, null, null, NaN3, NaN3, 'NaN3', 0, 0, 'A', 'A', {}, {}];
the console.log (UNIQUE (ARR))
// [. 1, "to true", to true, 15 , false, undefined, null, " NaN", 0, "a", {...}, {...}]

The recursive
function UNIQUE (ARR) {
var Array = ARR;
var = len be array.length;
Array.sort (function (A, B) {// Sort the weight easier to
return A - B;
})
function Loop (index) {
IF (index> =. 1) {
IF (Array [index] === Array [index-. 1]) {
Array.splice (index,. 1);
}
loop (index -. 1); // recursive loop and then to re-array
}
}
Loop (-len. 1);
return array;
}
var ARR = [1,1, 'to true', 'to true', to true, to true, 15, 15, to false, to false, undefined, undefined, null, null, NaN3, NaN3, 'NaN3', 0, 0, 'A', 'A', {}, {}];
the console.log (UNIQUE (ARR))
// [. 1, "A", " true ", true, 15, false , 1, {...}, null, NaN, NaN," NaN ", 0," a ", {...}, undefined]

6. The use of map data structure to a weight
function arrayNonRepeatfy (ARR) {
the let new new map the Map = ();
the let the Array Array = new new (); // array for returning results
for (let i = 0; i <arr.length ; I ++) {
IF (Map .has (ARR [I])) {// if the key value
Map .set (ARR [I], to true);
} the else {
Map .set (ARR [I], to false) ; // if the key value is not
Array .push (ARR [I]);
}
}
return Array;
}
var ARR = [1,1, 'to true', 'to true', to true, to true, 15, 15, to false, to false, undefined, undefined, null, null, NaN3, NaN3, 'NaN3', 0, 0, 'A', 'A', {}, {}];
the console.log (UNIQUE
[. 1, "A", " true ", true, 15, false , 1, {...}, null, NaN, NaN," NaN ", 0," a ", {...}, undefined]

Guess you like

Origin www.cnblogs.com/lihemengbky/p/12550153.html