Javascript implements multiple methods for Array deduplication, and javascript implements arrays.
Don't talk nonsense, just pick up the dry goods!
Let's talk about the requirements of this instance: Write a method to implement de-duplication of arrays. (Requirement: Execute the method, pass an array, and return the new array after deduplication. The original array remains unchanged. During implementation, only one layer of loops can be used, and double-layer nested loops can also be written for reference only );
Let's explain to beginners what is array deduplication (old bird skips): this means to remove repeated elements in the array, for example, var arr = [,]; the new array obtained by array deduplication is [3, 2, 4, 1], which is such a function.
The implementation method is relatively simple, and there are many implementation methods. Many Daniel has also written related articles. The reason why I write this blog is that I want to forget it, the second is to give beginners a better understanding of its implementation principles. Well, let's look at the first implementation method:
First, traverse the new array
Var arr = [1, 'B', 'B',]; // The first Array. prototype. unique1 = function () {var arr1 = []; // defines a new array for (var I = 0; I <this. length; I ++) {if (arr1.indexOf (this [I]) =-1) {// determine whether arr1.push (this [I]) exists in the original number group in the target array;} return arr1;} console. log (arr); // [1, 'B', 'B',] console. log (arr. unique1 (); // [1, "B", 4, 3, 5] // The main idea of this method is to create an array and then in the original array, starting from the first one, check whether this element exists in the new array. If yes, ignore the element and proceed to the next one. If no, save the element to the new array. // That is to say, each comparison traverses the new array until the same element is found, which is performance-consuming.
If you are not familiar with this writing method, you can change it to the following one, with the same effect:
Var arr = [1, 'B', 'B', 4,3, 3,4, 5, 1]; function unique1 (arr) {var arr1 = []; for (var I = 0; I <arr. length; I ++) {if (arr1.indexOf (arr [I]) =-1) {// determine whether arr1.push (arr [I]) exists in the original number group in the target array;} return arr1;} console. log (arr); // [1, 'B', 'B',] console. log (unique1 (arr); // [1, "B", 4, 3, 5]
I will not rewrite the following method. You can rewrite the method based on the above format, and I will not output the result because the result is the same. Write the comments in the Code and try again.
Second, it is implemented through the hash table (this concept is a bit big, and the specific principle is not described here. I will write it again if I have time. This is a good thing ).
Var arr = [1, 'B', 'B', 4,3, 3,4, 5, 1]; Array. prototype. unique2 = function () {var hash = {}; // defines a hash table var arr1 = []; // defines a new array for (var I = 0; I <this. length; I ++) {/* It is difficult to understand here. We can see step by step that hash is an object and key-value pairs (key: value) exist ), however, it is empty now, so hash [key] = value; Step 1: I = 0; this [I] = this [0] = 1; hash [this [0] = hash [1]. Because hash is empty at first and the value of key = 1 is not found, undefined and execute the next step: hash [1] = true (in this case, the hash object has the first key-Value Pair). Add the first number of the original array to the new array. Repeat the first step because it does not repeat has. The values of h are all undefined, and the repeated values are all true. Therefore, they are not repeatedly added to the new array. Because the values stored in the hash table are saved addresses, they are placed in the heap memory, therefore, the number of non-repeated elements must be stored in memory. Therefore, this method occupies the memory, but in contrast, this operation is the fastest, this means that the space is used for time, and the data size is small. We recommend this method */if (! Hash [this [I]) {hash [this [I] = true; arr1.push (this [I]) ;}return arr1 ;} console. log (arr); console. log (arr. unique2 ());
Third, it is implemented by traversing its own locations for consistency
Var arr = [1, 'B', 'B', 4,3, 3,4, 5, 1]; Array. prototype. unique3 = function () {var arr1 = []; // defines a new array for (var I = 0; I <this. length; I ++) {if (this. indexOf (this [I]) = I) {// here it is also indexOf traversal. From the position of the first element in the original array, if the position displayed for the first time is equal to the subscript, it indicates that the current element is not repeated. If not, it indicates that arr1.push (this [I]) has already appeared before this element; }}return arr1;} console. log (arr); console. log (arr. unique3 ());
Fourth, this is a bit interesting. It can only be used in special cases, that is, sorting with arrays first, then comparing with 22, and outputting a new sorted Array
Array. prototype. unique4 = function () {/* here is the idea: first sort (from small to large by default), then the first of the original array to the new array, because it is sorted, therefore, the repeat will only exist in the adjacent position, which is equivalent to doing 22 comparison. If they are equal, the next group will be performed. If they are not equal, the number will be saved to the new array, use this number for comparison */this. sort (); var arr1 = [this [0]; for (var I = 1; I <this. length; I ++) {if (this [I]! = Arr1 [arr1.length-1]) {arr1.push (this [I]);} return arr1;} console. log (arr); console. log (arr. unique4 ());
Wow, close the job!
The requirement also says that double-layer nested loops can be used for implementation, instead of two-layer for loops, so that each of them can be compared with the original array.
Array. prototype. unique5 = function () {// double-layer loop, one-to-one comparison for (var I = 0; I <this. length; I ++) {// starts from 0 for (j = I + 1; j <this. length; j ++) {// start from 1 and compare if (this [I] === this [j]) One by one {// if this. splice (j, 1); // Delete this element} return this;} console. log (arr); console. log (arr. unique5 ());
This writing method involves too many cycles and is not recommended. Some people may say that the first and third types are not repeated every time? Is it about the same as 5th? Yes, you can understand it. It means that you understand it, but it is not a special understanding. If we say it is almost the same, it will be much worse, indexOf () it indicates that when the first matching element is found
Stop traversal, and the 5th types will traverse the entire array no matter whether it is not found. If there is a large amount of data, which of the following performance do you think is better?
Note: If the values in the comparison are full or unequal, always use constant (=) and not constant (! =), Because this involves the element type, for example, 1 and '1' are not constant!
The above is really dry, and there is no water at all. You can only understand it by yourself!
Articles you may be interested in:
- Implementation Code for repeated entries in javascript numeric Array
- Two common methods and code for de-duplicating javascript Arrays
- Performance Test and comparison of three methods for removing javascript Arrays
- Detailed overview of sorting and array deduplication in js Algorithms
- Js implements array deduplication to determine whether the array and object content are the same
- Two array de-duplicated JS Code
- Summary of three common methods of js array deduplication
- Summary of js array deduplication