During project development, we often need some algorithms about javascript arrays, such as array deduplication, array intersection, array disruption, and so on. Today, I will share my summary algorithms with you. 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.
I. array deduplication
Method 1:
// Function unique (arr) {var result = []; for (var I = 0; I <arr. length; I ++) {if (result. indexOf (arr [I]) =-1) result. push (arr [I]);} return result ;}
Method 2:
// 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] function unique (arr) {var hash = {}, result = []; for (var I = 0; I <arr. length; I ++) {if (! Hash [arr [I]) {hash [arr [I] = true; result. push (arr [I]) ;}} return result ;}
Method 3:
// The sorting result is adjacent. If the sorting result is the same, discard it. Otherwise, add the result. The problem is the same as that in method 2. If the array contains, '1', an error occurs.
function unique (arr) { arr.sort(); var result=[arr[0]]; for(var i = 1; i < arr.length; i++){ if( arr[i] !== arr[i-1]) { result.push(arr[i]); } } return result;}
Method 4:
// The simplest but least efficient algorithm does not show the bugfunction unique (arr) {if (arr. length = 0) return; var result = [arr [0], isRepeate; for (var I = 0, j = arr. length; I <j; I ++) {isRepeate = false; for (var k = 0, h = result. length; k
Method 5:
// This method makes full use of the recursion and indexOf methods. Thanks to @ var unique = function (arr, newArr) {var num; if (-1 = arr. indexOf (num = arr. shift () newArr. push (num); arr. length & unique (arr, newArr );}
2. array Order Disruption
Method 1:
// Randomly extract a number and move it to the new array. function shuffle (array) {var copy = [], n = array. length, I; // if there are still elements, continue... While (n) {// randomly extract an element I = Math. floor (Math. random () * array. length); // if this element has not been selected before .. If (I in array) {copy. push (array [I]); delete array [I]; n -- ;}} return copy ;};
Method 2:
// Similar to method 1, except that function shuffle (array) {var copy = [], n = array is removed by splice. length, I; // if there are still elements .. While (n) {// randomly select an element I = Math. floor (Math. random () * n --); // move to the new array to copy. push (array. splice (I, 1) [0]);} return copy ;}
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:
// The built-in isArray method var array6 = []; Array. isArray (array6); // true
Method 2:
// Use the instanceof operator var array5 = []; array5 instanceof Array; // true
Method 3:
// Use the return value of toString function isArray (o) {return Object. prototype. toString. call (o) ==' [object Array] ';}
4. Intersection of Arrays
Method 1:
// Use the indexOf method array1.filter (function (n) {return array2.indexOf (n) that comes with the filter and array methods )! =-1 });
V. array Union
Method 1:
// Method principle: connect two arrays and deduplicate function arrayUnique (array) {var a = array. concat (array2); for (var I = 0; I6. array difference set
Method 1:
// Use the filter and indexOf Methods Array. prototype. diff = function (a) {return this. filter (function (I) {return. indexOf (I) <0 ;});};
The preceding method 1 can only find the difference set between one array and the other. For example, array1.diff (array2) can only find the difference set between array1 and array2, if you want to obtain different values of the two arrays, you can use array1.diff (array2 ). concat (array2.diff (array1), you can also use method 2
Method 2
var array1 = new Array(55,55,88,6,68,109,55,33,6,2,1); var array2 = [55,88,99,69,109,55,33,6,2,1]; var diffArr = array1.concat(array2); var diff = diffArr.filter(function(i) { return array1.indexOf(i) < 0||array2.indexOf(i) < 0; }); console.log( diff );
This point has been summarized for the time being and will be further supplemented. If you have any questions, please leave a message for further discussion and progress. ^_^