Array. prototype. unique1 = function (){
Var arr = [];
For (var I = 0; I <this. length; I ++ ){
// Check whether there are any arrays in it. If not, put them in.
If (arr. indexOf (this [I]) =-1 ){
Arr. push (this [I])
}
}
Return arr;
}
Array. prototype. unique2 = function (){
Var arr = [],
Json = {};
For (var I = 0; I <this. length; I ++ ){
// Use a hash table and use keywords to determine deduplication
// If no current entry exists in the hash table
If (! Json [this [I]) {
Json [this [I] = true;
Arr. push (this [I]);
}
}
Return arr;
}
Array. prototype. unique3 = function (){
This. sort (req); // sort the array first.
Var arr = [];
For (var I = 0; I <this. length; I ++ ){
// If the current item is not the same as the previous one, the result array is saved.
If (this [I]! = This [i-1]) {
Arr. push (this [I]);
}
}
Return arr;
}
The indexOf method of the array is used in both the 1st and 3rd methods. The purpose of this method is to find the location where the saved parameter appears for the first time in the array. Obviously, the js engine will traverse the array when implementing this method until it finds the target. Therefore, this function will waste a lot of time. The method in 2nd uses the hash table. Store existing objects in the form of a subobject. The reference of the underlying object is much faster than the search for an array using indexOf.
To determine the efficiency of these three methods, I made a test program to generate an array consisting of a random number of 10000 characters and then used several methods to test the execution time. The result shows that the second method is much faster than the other two methods. However, there should be more than one method for memory usage because one hash table is added. This is the so-called space change time. This is the test page. You can also check it.
I wrote the fourth method.
Method 4
Array. prototype. unique4 = function ()
{
This. sort ();
Var re = [this [0];
For (var I = 1; I <this. length; I ++)
{
If (this [I]! = Re [re. length-1])
{
Re. push (this [I]);
}
}
Return re;
}
Use indexOf to determine the new array
• Similar indexOf is actually used in underscore. js
// Input an array
Function unique1 (arr ){
Var tmpArr = [];
For (var I = 0; I <arr. length; I ++ ){
// If the I of the current array has been saved as a temporary array, skip this step,
// Otherwise, push the current item to the temporary array.
If (tmpArr. indexOf (arr [I]) =-1 ){
TmpArr. push (arr [I]);
}
}
Return tmpArr;
}
Use indexOf to determine the old array
Function unique2 (arr ){
Var tmpArr = []; // result array
For (var I = 0; I <arr. length; I ++ ){
// If the position where the I entry of the current array appears for the first time in the current array is not I,
// This indicates that the I-th item is repeated and ignored. Otherwise, the result array is saved.
If (arr. indexOf (arr [I]) = I ){
TmpArr. push (arr [I]);
}
}
Return tmpArr;
}
Search by hash
• The implementation of JS objects is the feature of hash tables.
Function unique3 (arr ){
Var tmpArr = [], hash = {}; // hash is a hash table
For (var I = 0; I <arr. length; I ++ ){
If (! Hash [arr [I]) {// if the hash table does not have the current item
Hash [arr [I] = true; // save it to the hash table
TmpArr. push (arr [I]); // save it to a temporary array
}
}
Return tmpArr;
}
Array extension
Array. prototype. unique1 = function (){
Var tmpArr = [];
For (var I = 0; I <this. length; I ++ ){
If (tmpArr. indexOf (this [I]) =-1 ){
TmpArr. push (this [I]);
}
}
Return tmpArr;
}
Array. prototype. unique2 = function (){
Var tmpArr = []; // result array
For (var I = 0; I <this. length; I ++ ){
If (this. indexOf (this [I]) = I ){
TmpArr. push (this [I]);
}
}
Return tmpArr;
}
Array. prototype. unique3 = function (){
Var tmpArr = [], hash = {};
For (var I = 0; I <this. length; I ++ ){
If (! Hash [this [I]) {
Hash [this [I] = true;
TmpArr. push (this [I]);
}
}
Return tmpArr;
}
Use Set
• Set and Map are new data structures in ES6
• Set can directly store a Set of keys that are not repeated. This key can also be an object or a string.
Create a set
Var s = new Set ([1, 2, 3,]);
S; // Set {1, 2, 3}
Add element
>>> S. add (4)
>>> S
{1, 2, 3, 4}
>>> S. add (4)
>>> S
{1, 2, 3, 4} // duplicate elements are not added
Delete element
S; // Set {1, 2, 3, 4}
S. delete (3 );
S; // Set {1, 2, 4}
Traversal element
• Map and Set cannot use subscript
• The ES6 standard introduces the new iterable type. Array, Map, and Set all belong to the iterable type.
Var s = new Set (['A', 'B', 'C']);
For (var x of s) {// traverse Set
Alert (x );
}
• You can also directly use the iterable built-in forEach method.
• The forEach method is introduced in the ES5.1 standard.
Var s = new Set (['A', 'B', 'C']);
S. forEach (function (element, set ){
Alert (element );
});