Detailed explanation of the javascript array deduplication problem, detailed explanation of the javascript Array
First, I want to create another result array to store non-duplicated data in the original array. Traverse the original array and compare it with the elements in the result array to check whether the elements are repeated. As a result, I wrote the following:Code:
Array.prototype.clearRepetitionA = function(){ var result = []; var isRepetition; for(var i=0; i<this.length; i++){ isRepetition = false; for(var j=0; j<result.length; j++){ if(this[i] === result[j]){ isRepetition = true; break; } } if(!isRepetition){ result.push(this[i]); } } return result; }
After writing, I suddenly remembered that the array method indexOf in ECMAScript 5 I just read a few days ago can retrieve array elements. So I used the indexOf method to replace the second loop and wrote the following:Code B:
Array.prototype.clearRepetitionB = function(){ var result = []; for(var i=0; i<this.length; i++){ if(result.indexOf(this[i]) == -1){ result.push(this[i]); } } return result; }
The Code suddenly changed from 17 lines to 9 lines, which is much more concise. There are generally more than one major mathematical question solutions for the third year, and I will continue to think about other methods. The indexOf method searches for elements with Given Values in the entire array, returns the index of the first element found, and returns-1 if no value is found. The first parameter is the value to be searched, the second parameter is optional. It specifies an index in the array and searches from there. If this parameter is omitted, searches from the beginning. When the thinking goes off, I think that the previous method is to check whether the values are repeated, now that the indexOf method is available, you can determine whether to repeat each element by comparing its index value with its own index value when each element is detected for the first time. So I wrote it again.Code C:
Array.prototype.clearRepetitionC = function(){ var result = [this[0]]; for(var i=1; i<this.length; i++){ if(this.indexOf(this[i]) == i){ result.push(this[i]); } } return result; }
After writing this, I continued to think about it. I really couldn't think of other methods. These three methods are very basic methods. So I checked the answer and checked myself. Looking at the answer, I found myself still too weak, and there are still some whimsical questions. The following is not what I think, so I will not talk too much about my mental journey. Not much nonsense. Let's talk about the classic answer + analysis.
First, let's talk about the space-for-time solution in an algorithm. To maintain the formation, we call itCode DRight:
Array.prototype.clearRepetitionD = function(){ var result = []; var obj = {}; var key,type; for(var i=0; i<this.length; i++){ key = this[i]; type = typeof key; if(!obj[key]){ obj[key] = [type]; result.push(key); }else if(obj[key].indexOf(type)){ obj[key].push(type); result.push(key); } } return result; }
In this method, when traversing the original array, an object obj attribute is used to save the element values in the original array. At the same time, the value of this attribute is an array used to store the type of this attribute. This can separate the elements in the original array that are similar to the number 1 element from the string '1. This method reduces the time spent by the indexOf method in the above three methods by building an additional object. It can be said that this method is more efficient.
If you are satisfied with the above-mentioned efficient method of changing the space for time without continuing to watch it, it is a big mistake. Now, there is no doubt thatCode ENow:
Array.prototype.clearRepetitionE = function(){ var result = []; for(var i=0; i<this.length; i++){ for(var j=i+1; j<this.length; j++){ if(this[i] === this[j]){ j = ++i; } } result.push(this[i]); } return result; }
Code D changes the time of space, and it feels like a normal one. What about code E? This code is wrong. Can this really be heavy? Yes. I didn't understand the code at first. I understood it after reading the parsing again. If you do not understand it, you need to carefully analyze it: the first layer traverses the original array from the back, and the second layer loop is to check whether each element is repeated with the element after it, if there is a repeating element after it, skip it. If all the elements after this element are not repeated with it, add it to the result array. The implementation idea of this method is: Get the rightmost value without repetition and add it to the result array. This method also optimizes the loop of the second layer compared with the first method, and the efficiency is higher than it, however, the element sequence in the result array of this method is different from that in the original array.
After reading code E parsing, have you reached your thumb and put your eyes on it? (Don't give me these flowers and honors. You should give them to the experts who write this method ). Let's talk about it later.The last method:That is, sort the data first, and then duplicate the data. Old rule, it is calledCode F:
Array.prototype.clearRepetitionF = function(){ this.sort(); var result = [this[0]]; for(var i=1; i<this.length; i++){ if(this[i] !== result[result.length-1]){ result.push(this[i]); } } return result; }
In this case, sort the array elements by the array sorting method sort, and then deduplicate the elements.
The above isDeduplication of javascript ArraysI studied the problem step by step and constantly improved the code. I shared a total of six pieces of code. I hope you will learn it carefully and gain some benefits.