1. Traverse array method
It is the easiest way to deduplicate an array (indexOf method)
Implementation idea: Create a new array, traverse the array to be repeated, and add it to the new array when the value is not in the new array (indexOf is -1);
var arr=[2,8,5,0,5,2,6,7,2];
function unique1(arr){
var hash=[];
for (var i = 0; i < arr.length; i++) {
if(hash.indexOf(arr[i])==-1){
hash.push(arr[i]);
}
}
return hash;
}
2. Array subscript judgment method
Call the indexOf method, the performance is similar to method 1
Implementation idea: If the first occurrence of the i-th item of the current array is not i in the current array, it means that the i-th item is repeated and ignored. Otherwise, store the result array.
function unique2(arr){
var hash=[];
for (var i = 0; i < arr.length; i++) {
if(arr.indexOf(arr[i])==i){
hash.push(arr[i]);
}
}
return hash;
}
3. Adjacent removal method after sorting
Implementation idea: sort the incoming array, the same values will be adjacent after sorting, and then when traversing the sorted array, the new array will only add values that do not duplicate the previous value.
function unique3(arr){
arr.sort();
var hash=[arr[0]];
for (var i = 1; i < arr.length; i++) {
if(arr[i]!=hash[hash.length-1]){
hash.push(arr[i]);
}
}
return hash;
}
4. Optimize traversal array method (recommended)
Implementation idea: double-layer loop, the outer loop represents from 0 to arr.length, and the inner loop represents from i+1 to arr.length
Put the non-repeated right-hand values into a new array. (When a duplicate value is detected, the current loop is terminated and the next round of judgment of the outer loop is entered)
function unique4(arr){
var hash=[];
for (var i = 0; i < arr.length; i++) {
for (var j = i+1; j < arr.length; j++) {
if(arr[i]===arr[j]){
++i;
}
}
hash.push(arr[i]);
}
return hash;
}
5. ES6 implementation
Basic idea: ES6 provides a new data structure Set. It is similar to an array, but the values of the members are all unique and there are no duplicate values.
The Set function can accept an array (or array-like object) as a parameter for initialization.
function unique5(arr){ var x = new Set(arr); return [...x]; }
extension: if duplicate, remove the element
Array subscript deduplication
function unique22(arr){
var hash=[];
for (var i = 0; i < arr.length; i++) {
if(arr.indexOf(arr[i])==arr.lastIndexOf(arr[i])){
hash.push(arr[i]);
}
}
return hash;
}