Javascript array de-duplicated summary, javascript Array Summary
Preface
Recently, in order to change jobs, I prepared for an interview and began to review JavaScript-related knowledge. Yesterday afternoon I thought of array deduplication-related methods. I simply compiled several JavaScript algorithm articles for future use, in this series of articles, there may be no number of articles, no time, no time to write, no correctness, and no efficiency. I just want to talk about my understanding. If there is any mistake, please make an axe.
About deduplication
Array deduplication is a common algorithm inspection point. Unique and non-unique methods are used to remove duplicates. Simply put, you can pick out the unique one or delete the unique one. All of the following algorithms are named by myself. Ignore them.
Loop matching deduplication
As the name suggests, each element in the array is compared with the array storing elements. When an element that is not repeated is encountered, it is placed into the new array until the loop ends, because the matching also has a loop, it can also be called double loop matching deduplication, which is also the simplest method that everyone can think.
Implementation Code:
Var arr = [,]; var result = []; // match function isMatch (array, n) {for (var I = 0; I <array. length; I ++) {if (array [I] = n) {return true ;}} return false ;}; // verify all element functions unqiue (array) {for (var I = 0; I <array. length; I ++) {if (! IsMatch (result, array [I]) {result. push (array [I]) ;}} return result ;}; console. log (unqiue (arr ));
Note: There is a bug in the above method. When there is a string in the form of numbers and numbers, there is no distinction between a number and a number string. Because the matching function isMatch () uses a pair of equal "=" and does not validate the element type, we should actually use equal "= ".
The modified code is as follows:
Var arr = [, 3, '1', 7]; var result = []; // matches function isMatch (array, n) {for (var I = 0; I <array. length; I ++) {if (array [I] = n) {return true ;}} return false ;}; // verify all element functions unqiue (array) {for (var I = 0; I <array. length; I ++) {if (! IsMatch (result, array [I]) {result. push (array [I]) ;}} return result ;}; console. log (unqiue (arr ));
Algorithm advantages and disadvantages:
Advantages:
Simple implementation and intuitive thinking
Disadvantages:
Low Efficiency
JSON deduplication/object deduplication/dictionary deduplication
JSON deduplication is simply to use the uniqueness of the Object key to convert the array elements to JSON or the Object key value. The JSON value stores the index of the array, and then performs a for in loop on the JSON object to store it in the new array.
Array, JSON, and {} are all objects, so this algorithm can be implemented using any one.
Implementation Code:
Array method:
Var arr = [, 3, '1', 7]; function unqiue (array) {var cache = []; var result = []; // convert the array element into the object's key for (var I = 0; I <array. length; I ++) {cache [array [I] = I ;}; // stores the key (the actual array element) for (key in cache) {result. push (key) ;}; return result ;}console. log (unqiue (arr ));
JSON mode:
Var arr = [, 3, '1', 7]; function unqiue (array) {var cache ={}; var result = []; // convert the array element into the object's key for (var I = 0; I <array. length; I ++) {cache [array [I] = I ;}; // stores the key (the actual array element) for (key in cache) {result. push (key) ;}; return result ;}console. log (unqiue (arr ));
Object method:
Var arr = [, 3, '1', 7]; function unqiue (array) {var cache = new Object (); var result = []; // convert the array element into the object's key for (var I = 0; I <array. length; I ++) {cache [array [I] = I ;}; // stores the key (the actual array element) for (key in cache) {result. push (key) ;}; return result ;}console. log (unqiue (arr ));
Algorithm advantages and disadvantages:
Advantages:
Simple
High Efficiency
Disadvantages:
1. Changed the type of array elements ()
2. There is a bug (it is nothing more than a string of numbers and numbers)
Recursive deduplication of queues
Last night, I thought for a long time that I would like to use the queue method to sort the arrays into ascending or descending queues so that the same elements are in a region and then match them forward from the end of the team, if the match succeeds, the team end is deleted and the previous element matches the previous one. After the matching is complete, the remaining elements are the deduplicated queues.
Var arr = [6, 4, 6, 9, '6', 13, 56, 9, '11', 1, 8, '7', 17, 5, 45, 3, 7]; function unqiue (array) {// sort the array to form the queue array. sort (function (m, n) {return m-n ;}); // after sorting, the team end is compared forward. If the same, delete the team end, function loop (Index) {if (Index> = 1) {if (array [Index] = array [Index-1]) {arr. splice (Index, 1);} loop (Index-1);} loop (array. length-1); return array;} console. log (unqiue (arr ));
Algorithm advantages and disadvantages:
Advantages:
High Efficiency
Disadvantages:
Inefficient
INDEXOF deduplication Method
Determine whether the browser supports indexOf. If indexOf is ecmaScript5, the new method IE8 or lower (IE8 and IE8 only support part of ecma5) is not supported.
If (! Array. prototype. indexOf) {// added the indexOf method Array. prototype. indexOf = function (item) {var result =-1, a_item = null; if (this. length = 0) {return result;} for (var I = 0, len = this. length; I <len; I ++) {a_item = this [I]; if (a_item = item) {result = I; break ;}} return result ;}}
Articles you may be interested in:
- Detailed explanation of javascript Array deduplication
- Five methods to de-duplicate JavaScript Arrays
- Three methods and code examples for removing the deduplication of JavaScript Arrays
- Summary of three common methods of js array deduplication
- Js implements array deduplication to determine whether the array and object content are the same
- Detailed overview of sorting and array deduplication in js Algorithms
- Js retrieves the intersection of two arrays | difference set | Union set | supplement set | deduplication sample code
- Performance Test and comparison of three methods for removing javascript Arrays
- Two array de-duplicated JS Code
- How to remove duplicate values in JavaScript arrays and strings