“This is the 12th day of my participation in the First Challenge 2022. For details: First Challenge 2022”
Hello, everyone, I touch fish small public, the real strong, will not complain, if you do not want to be looked down upon by others, you have to pay more than others ten times a hundred times more effort, to stand higher than others. Find the length of the smallest string in a string that does not contain repeated characters. Today we are going to learn ten ways to js weight.
I’m going to give you an array
Var arr =,3,3,4,5,5,4,6,3 [2]Copy the code
The first method uses Set
var arr2=[...new Set(arr)]
console.log(arr2) // [2, 3, 4, 5, 6]
Copy the code
The execution time is as follows
The second method uses array. from and Set
var arr2=Array.from(new Set(arr))
console.log(arr2) // [2, 3, 4, 5, 6]
Copy the code
The execution time is as follows
The third method uses Map objects
const map = new Map(); var arr2 = []; for (var i = 0; i < arr.length; i++) { if (! Map. has(arr[I])) {// Find if there is a key map.set(arr[I], true) // Add key and value console.log(map) arr2.push(arr[I]); } } console.log(arr2) // [2, 3, 4, 5, 6]Copy the code
The execution time is as follows
The fourth method makes use of array.includes
var arr2 = [] for (j = 0; j< arr.length; j++) { if(! arr2.includes(arr[j])){ arr2.push(arr[j]) } } console.log(arr2) // [2, 3, 4, 5, 6]Copy the code
The execution time is as follows
The fifth method uses array.sort
arr = arr.sort() //[2, 3, 3, 3, 4, 4, 5, 5, 6] var arr2= [arr[0]]; For (var I = 1; var I = 1; i < arr.length; i++) { if (arr[i] ! == arr[i-1]) { arr2.push(arr[i]); } } console.log(arr2) // [2, 3, 4, 5, 6]Copy the code
The execution time is as follows
The sixth method is a double cycle of comparison
for (let i = 0; i < arr.length; i++) { for (let j = 0; j < arr.length; j++) { if(arr[i]===arr[j] && i! Arr. Splice (j,1)}} console.log(arr) // [2, 3, 4, 5, 6]Copy the code
The execution time is as follows
The seventh method uses array.indexof
var arr2 = []
for (j = 0; j< arr.length; j++) {
if(arr2.indexOf(arr[j])==-1){
arr2.push(arr[j])
}
}
console.log(arr2) // [2, 3, 4, 5, 6]
Copy the code
The execution time is as follows
The eighth method uses array.filter
Var arr2=arr.filter((item,index)=>{return arr.indexof (item)===index}) console.log(arr2) // [2, 3, 4, 5, 6]Copy the code
The execution time is as follows
The ninth method uses the idea of double loops and switches
var arr2 = []
var flag = false
for (j = 0; j< arr.length; j++) {
for (k = 0; k < arr2.length; k++) {
if (arr[j] == arr2[k]) {
flag = true;
}
}
if (flag == false) {
arr2.push(arr[j])
}
flag = false
}
console.log(arr2) // [2, 3, 4, 5, 6]
Copy the code
The execution time is as follows
The tenth method uses array.sort and recursion
Function loop(index){if(index >= 1){if(arr[index] === arr[index-1]){if(arr[index] == arr[index-1]){if(arr[index] == arr[index-1]){if(arr[index] == arr[index-1]){if(arr[index] == arr[index-1]) arr.splice(index,1); } loop(index - 1); }} loop(arr. Length -1); Log (arr2) // [2, 3, 4, 5, 6]Copy the code
The execution time is as follows
Conclusion:
There are far more than these ten methods to remove duplicate, here is in order of running time from least to most, the more late the performance is worse, recommended to use the first two methods code simplicity does not affect the performance. Well, this is the end of the article, welcome everyone (like + comment + attention) have a question can exchange with each other. I hope this article will be helpful to you and I hope you can support me more. Today is the 12th day of my participation in the first Wenwen Challenge 2022. Come on!