Summary of common javascript Array Algorithms
We are often asked about some javascript Array algorithms, such as array deduplication, array intersection, array disruption, and so on, whether during the interview or in the written test. Today, I will take some time to summarize some of the commonly used array algorithms in javascript, so that you can use them in the interview test or daily development process. Some of the algorithms are from the network. Here we summarize them. At the end of the article, I will attach the reference source. If you look at the algorithm directly, you can go to the references to see it. The explanation is very good. 1. array deduplication Method 1:
1 // use the indexOf method 2 function unique (arr) {3 var result = []; 4 for (var I = 0; I <arr. length; I ++) 5 {6 if (result. indexOf (arr [I]) =-1) result. push (arr [I]); 7} 8 return result; 9}
Method 2:
1 // If the hash table is used, an error may occur if the string and number are the same, for example, var a = [1, 2, 3, 4, '3', 5]. [1, 2, 3, 4, 5] 2 function unique (arr) 3 {4 var hash = {}, result = []; 5 for (var I = 0; I <arr. length; I ++) 6 {7 if (! Hash [arr [I]) 8 {9 hash [arr [I] = true; 10 result. push (arr [I]); 11} 12} 13 return result; 14}
Method 3:
1 // after sorting, It is adjacent. If the same, it is abandoned; otherwise, it is added to the result. The problem is the same as that in method 2. If the array contains, '1', the error 2 function unique (arr) {3 arr will occur. sort (); 4 var result = [arr [0]; 5 for (var I = 1; I <arr. length; I ++) {6 if (arr [I]! = Arr [I-1]) {7 result. push (arr [I]); 8} 9} 10 return result; 11}
Method 4:
1 // The simplest but least efficient algorithm, and no bug 2 function unique (arr) {3 if (arr. length = 0) return; 4 var result = [arr [0], isRepeate; 5 for (var I = 0, j = arr. length; I <j; I ++) {6 isRepeate = false; 7 for (var k = 0, h = result. length; k
2. method 1 of array order disturbance:
1 // randomly extract a number each time and move it to the new array. 2 function shuffle (array) {3 var copy = [], 4 n = array. length, 5 I; 6 // if there are still elements, continue... 7 while (n) {8 // randomly extract an element 9 I = Math. floor (Math. random () * array. length); 10 // if this element has not been selected before .. 11 if (I in array) {12 copy. push (array [I]); 13 delete array [I]; 14 n --; 15} 16}
Method 2:
1 // similar to method 1, except that splice is used to remove the option 2 function shuffle (array) {3 var copy = [], 4 n = array. length, 5 I; 6 // if there are still elements .. 7 while (n) {8 // randomly select an element 9 I = Math. floor (Math. random () * n --); 10 // move to 11 copy in the new array. push (array. splice (I, 1) [0]); 12} 13 return copy; 14}
Method 3:
// The numbers of the preceding random beats are exchanged with the numbers at the end in sequence, and the numbers are moved forward in sequence. That is, the first n counts are randomly swapped with the nth number, the first n-1 number is exchanged with the N-1 number, and so on. Function shuffle (array) {var m = array. length, t, I; // if there are still elements... While (m) {// randomly select an element... I = Math. floor (Math. random () * m --); // exchange with the current element t = array [m]; array [m] = array [I]; array [I] = t ;} return array ;}
3. Array Judgment Method 1: 1 // built-in isArray method 2 var array6 = []; 3 Array. isArray (array6); // true Method 2: 1 // use the instanceof operator 2 var array5 = []; 3 array5 instanceof Array; // true method 3: 1 // use the return value of toString 2 function isArray (o) {3 return Object. prototype. toString. call (o) = '[object Array]'; 4} 4. method 1 for intersection of Arrays: 1 // use the built-in indexOf method 2 array1.filter (function (n) {3 return array2.indexOf (n )! =-14}); 5. method 1 for Array union:
1 // method principle: connect two arrays and remove the weight of 2 function arrayUnique (array) {3 var a = array. concat (); 4 for (var I = 0; I <. length; ++ I) {5 for (var j = I + 1; j <. length; ++ j) {6 if (a [I] = a [j]) 7. splice (j --, 1); 8} 9} 10 11 return a; 12 };
6. method 1 of Array difference set: 1 // use filter and indexOf method 2 Array. prototype. diff = function (a) {3 return this. filter (function (I) {return. indexOf (I) <0 ;}); 4}; this point is summarized for the time being and will be further supplemented. You are welcome to add.