Code directly
<script> let arr = [2, 3, 4, 4, 6, 66, 4, 743, 243, 22, 2, 4]; //
One , Traversal array method , Create a new array , Traversing the original array , If it doesn't exist, put it in a new array , Finally, the new array is output function first(arr) { let array = []
for (let i = 0; i < arr.length; i++) { if (array.indexOf(arr[i]) == -1) {
array.push(arr[i]) } } return array; } // Adjacent removal after sorting , After sorting, traverse the original array , If the length -
1 The values of are different , Add it to the new array and output the new array function sortNum(a, b) { return a - b; } function
second(arr) { arr.sort(sortNum) let array = [arr[0]] for (let i = 0; i <
arr.length; i++) { if (arr[i] != array[array.length - 1]) { array.push(arr[i])
} } return array; } // Optimized traversal array method ( recommend ) function third(arr) { let array = []; for
(let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) { ++i } } array.push(arr[i]); } return array; } // ES6
set,set Methods by ES6 provide , The values inside are unique function setArr(arr) { let es6Set = new Set(arr)
return [...es6Set]; } console.log(' Traversal array method ') console.log(first(arr))
console.log(' Adjacent division after sorting ') console.log(second(arr)) console.log('ES6 set')
console.log(setArr(arr)) console.log(' Optimized traversal array method ') console.log(third(arr))
</script>
Array de duplication and sorting
let arr1 = [1, 25, 2, 26, 1234, 6, 213]; let arr2 = [2, 6, 2134, 6, 31, 623];
let c = [...new Set([...arr1, ...arr2])].sort((a, b) => { return a - b; });
//(10) [1, 2, 6, 25, 26, 31, 213, 623, 1234, 2134]
 

Technology