One, the simple way to heavy
- Create a new array, iterate through the incoming array, and push the value into the new array if it’s not there
- The indexOf method for arrays is not supported below Internet Explorer 8
1, function temp (array) {2, var temp = []; For (var I = 0; i < array.length; I++) {4, the if (temp. IndexOf (array [I]) = = 1) {5, temp. Push (array [I]); 6,} 7,} 8, return temp; If (1, 2, 2, 4, 9, 6, 7, 5, 2, 3, 5, 6, 5); 11, the console. The log (uniq (aa));Copy the code
Second, object key value method to remove weight
Fastest speed, takes up the most space (space for time)
- This method executes faster than any other method but takes up more memory.
- Create a new JS object and a new array. When traversing the array, determine whether the value is the key of the JS object.
- If not, add the key to the object and put it in a new array.
- Note: toString() is automatically executed on the incoming key to determine whether it is a JS object key.
- Different keys can be mistaken for the same, e.g. N [val]– n[1], n[“1”];
- To solve the above problem, “indexOf” is called again.
Function uniq(array){1, var temp = {}, r = [], len = array.length, val, type; For (var I = 0; i < len; Array [I] = array[I]; 5, type = typeof val; 6, the if (! Temp [val]) {7, temp[val] = [type]; 8, r.p ush (val); 9,} else if (temp[val].indexof (type) < 0) {10, temp[val].push(type); 11, r.p ush (val); 12,} 13,} 14, return r; 16, var aa = 15,} [1, 2, "2", 4, 9, "a", "a", 2,3,5,6,5]; 17, the console. The log (uniq (aa));Copy the code
Three, sorting after the adjacent division
- Sort the incoming array, and the same values are adjacent,
- The array is then iterated over, adding only values that do not duplicate the previous one.
- It will mess up the order of the original array
1, function uniq(array){2, array.sort(); 3, var temp = [array [0]]. 4, for(var I = 1; i < array.length; I ++){5, if(array[I]! == temp[temp.length-1]){6, temp. Push (array[I]); } 8,} 9, return temp; 10, 11, var aa =} [1, 2, "2", 4, 9, "a", "a", 2,3,5,6,5]; 12, the console. The log (uniq (aa));Copy the code
Array subscript method
- I still have to call indexOf with the same performance as method 1,
- If the first occurrence of the ith item in the current array is not I,
- So that means the ith term is repeated. Ignore it. Otherwise, store the result array.
1, function temp (array){2, var temp = []; For (var I = 0; i < array.length; I ++) {4, // If the first occurrence of the I item in the current array is at the position I; 6, if(array.indexof (array[I]) == I){7, temp. Push (array[I]) 8,} 9,} 10, return temp; 11,}, 12, var aa = [1, 2, "2", 4, 9, "a", "a", 2,3,5,6,5]; 13, the console. The log (uniq (aa));Copy the code
Optimization of traversal number group method
Recommended methods
- Get the unduplicated rightmost value and put it in a new array
- Method implementation code is pretty cool,
- Get the rightmost value that is not duplicated and put it in a new array.
- (Terminate the current cycle and enter the next round of judgment of the top cycle when duplicate values are detected)
1, function temp (array){2, var temp = []; Var index = []; Var l = array.length; 5, for(var I = 0; i < l; I ++) {6, for(var j = I + 1; j < l; If (array[I] === array[j]){8, I ++; 9, j = I; Temp. Push (array[I]); 13, index. Push (I); } 15, console.log(index); 16, return temp; If ((1,2,2,3, 3, 3,6,5)) 19, the console. The log (uniq (aa));Copy the code