When performing array operations, you often encounter the problem of removing duplicates. The following is a brief introduction to the method of deduplication of arrays.
indexOf deduplication
Array.prototype.unique1 = function() {var arr = [];for (var i = 0; i < this.length; i++) {var item = this[i];if (arr.indexOf(item) === -1) {arr.push(item);}}return arr;}[1,2,3,'4',3,4,3,1,'34',2].unique1(); //[1, 2, 3, "4", 4, "34"]However, under IE6-8, the indexOf method of array does not exist yet (although this is a bit old topic O(∩_∩)O~), but programmers have to write an indexOf method:
var indexOf = [].indexOf ? function(arr, item) {return arr.indexOf(item);} :function indexOf(arr, item) {for (var i = 0; i < arr.length; i++) {if (arr[i] === item) {return i;}}return -1;}Array.prototype.unique2 = function() {var arr = [];for (var i = 0; i < this.length; i++) {var item = this[i];if (arr.indexOf(item) === -1) {arr.push(item);}}return arr;}[1,2,3,'4',3,4,3,1,'34',2].unique2(); //[1, 2, 3, "4", 4, "34"]indexOf can also use this way of re-reconstruction:
Array.prototype.unique3 = function(){var arr = [this[0]]; for(var i = 1; i < this.length; i++) {if (this.indexOf(this[i]) == i){arr.push(this[i]);} } return arr;}[1,2,3,'4',3,4,3,1,'34',2].unique3(); //[1, 2, 3, "4", 4, "34"]Hash to reload
The above indexOf is correct, but in terms of performance, the double cycle will reduce performance. Then we use hash.
Array.prototype.unique4 = function() {var arr = [];var hash = {};for (var i = 0; i < this.length; i++) {var item = this[i];var key = typeof(item) + itemif (hash[key] !== 1) {arr.push(item);hash[key] = 1;}} return arr;}[1,2,3,'4',3,4,3,1,'34',2].unique4(); //[1, 2, 3, "4", 4, "34"]The core is to build a hash object to replace indexOf. Change space to time. Note that in JavaScript, the key value of an object can only be a string (of course, ES6 provides a Map data structure. It is similar to an object and is also a collection of key-value pairs, but the scope of "key" is not limited to strings. All types of values (including objects) can be regarded as keys. In other words, the Object structure provides a "string-value" correspondence, and the Map structure provides a "value-value" correspondence, which is a more complete Hash structure representation.), so var key = typeof(item) + item is needed to distinguish between the value 1 and the string '1'.
Then if you want '4' and 4 to be considered the same (the other approaches are the same)
Array.prototype.unique5 = function(){var arr=[];var hash={};for(var i=0,len=this.length;i<len;i++){if(!hash[this[i]]){ arr.push(this[i]);hash[this[i]]=true;}}return arr;}[1,2,3,'4',3,4,3,1,'34',2].unique5(); //[1, 2, 3, "4", "34"]After sorting, de-repeat
Array.prototype.unique6 = function(){this.sort();var arr = [this[0]];for(var i = 1; i < this.length; i++){if( this[i] !== arr[arr.length-1]){arr.push(this[i]);}}return arr;}[1,2,3,'4',3,4,3,1,'34',2].unique6(); //[1, 2, 3, "34", "4", 4]First sort the array, then compare two adjacent values. When sorting, use the JS native sort method, so it is very fast. There is only one drawback to this method. When comparing characters, they are sorted in the order of character encoding. So you will see that 10 is ranked first in 2. However, it will not affect the removal of heavy weight. However, to solve the problem of sort, the sort method accepts a parameter, which is a method:
function compare(value1,value2) {if (value1 < value2) {return -1;} else if (value1 > value2) {return 1;} else {return 0;}}[1,2,5,2,10,3,20].sort(compare); //[1, 2, 2, 3, 5, 10, 20]Set to reload
ES6 provides a new data structure Set. It's similar to an array, but the values of the members are all unique, without duplicate values. The browser is now fully supported, and the server node is also supported.
Array.prototype.unique7 = function(){return Array.from(new Set(this));}[1,2,3,'4',3,4,3,1,'34',2].unique7(); //[1, 2, 3, "4", 4, "34"]Method library
Recommended a method library Underscore.js, which is very popular in node or browser js.
const _ = require('underscore');_.uniq([1, 2, 1, 3, 1, 4]); //[1, 2, 3, 4]Test time
All of the above methods can be tested in a simple way, and then compare and select the best methods:
console.time("test");[1,2,3,'4',3,4,3,1,'34',2].unique7();console.timeEnd("test");==> VM314:3 test: 0.378msTo make the data bigger, create 1 million numbers randomly:
var arr = [];var num = 0;for(var i = 0; i < 1000000; i++){num = Math.floor(Math.random()*100);arr.push(num);}console.time("test");arr.unique7();console.timeEnd("test");The above is the JavaScript array deduplication that the editor introduced to you from slow to fast from medium to simple. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!