This article mainly introduces a variety of methods for implementing array de-duplication in javascript. If you are interested, you can refer to this article for a little bit of nonsense!
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
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; II 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
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
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
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 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!