-
Notifications
You must be signed in to change notification settings - Fork 0
/
数组去重.html
176 lines (166 loc) · 6.14 KB
/
数组去重.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
let arr = [1, 2, 4, 6, 9, 0, 2, 3, 45, 3, 2, 4]
//1.创建一个新数组,放入第一个元素,依次比较,若不同则加入新的数组
function fn1(arr) {
if (arr instanceof Array) {
if (arr.length == 0) return []
let res = [arr[0]]
for (let i = 0; i < arr.length; i++) {
let repeat = false
for (let j = 0; j < res.length; j++) {
if (arr[i] === res[j]) {
repeat = true
break
}
}
if (!repeat) {
res.push(arr[i])
}
}
return res
} else {
return
}
}
console.log(fn1(arr))
//2.先将原数组排序,在与相邻的比较,若不同则存入新数组
function fn2(arr) {
if (arr instanceof Array) {
if (arr.length == 0) return []
// srot()如果不带参数,是将按字母顺序对数组中的元素进行排序,也就是是按照字符编码的顺序进行排序。
arr = arr.sort((a, b) => {
return a - b
})
let res = [arr[0]]
for (let i = 1; i < arr.length; i++) {
if (arr[i] !== arr[i - 1]) {
res.push(arr[i])
}
}
return res
} else {
return
}
}
console.log(fn2(arr))
//3.利用对象属性的特性,如果没有该属性则存入数组
function fn3(arr) {
if (arr instanceof Array) {
if (arr.length == 0) return []
let res = []
let obj = {}
for (let i = 0; i < arr.length; i++) {
if (!obj[arr[i]]) {
res.push(arr[i])
obj[arr[i]] = true
}
}
return res
} else {
return
}
}
console.log(fn3(arr))
//4.利用数组的indexOf下标来查询
function fn4(arr) {
if (arr instanceof Array) {
if (arr.length == 0) return []
let res = []
for (let i = 0; i < arr.length; i++) {
if (res.indexOf(arr[i]) == -1) {
res.push(arr[i])
}
}
return res
} else {
return
}
}
console.log(fn4(arr))
//5.利用数组原型对象的includes方法
function fn5(arr) {
if (arr instanceof Array) {
if (arr.length == 0) return []
let res = []
for (let i = 0; i < arr.length; i++) {
if (!res.includes(arr[i])) {
res.push(arr[i])
}
}
return res
} else {
return
}
}
console.log(fn5(arr))
//6.利用数组原型对象的includes和filter方法
function fn6(arr) {
if (arr instanceof Array) {
if (arr.length == 0) return []
let res = []
res = arr.filter((item) => {
return res.includes(item) ? '' : res.push(item)
})
return res
} else {
return
}
}
console.log(fn6(arr))
//7.利用splice方法 依次遍历比较
function fn7(arr) {
if (arr instanceof Array) {
if (arr.length == 0) return []
let len = arr.length
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
if (arr[i] === arr[j]) {
arr.splice(j, 1)
j--
len--
}
}
}
return arr
} else {
return
}
}
console.log(fn7(arr))
//8.利用lastIndexOf
function fn8(arr) {
if (arr instanceof Array) {
if (arr.length == 0) return []
let res = [arr[0]]
for (let item of arr) {
res.lastIndexOf(item) == -1 ? res.push(item) : ''
}
return res
} else {
return
}
}
console.log(fn8(arr))
//9.利用set
function fn9(arr) {
if (arr instanceof Array) {
if (arr.length == 0) return []
let res = Array.from(new Set(arr))
return res
} else {
return
}
}
console.log(fn9(arr))
</script>
</body>
</html>