-
Notifications
You must be signed in to change notification settings - Fork 0
/
task3.js
43 lines (34 loc) · 1.04 KB
/
task3.js
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
// найти пересечение элементов двух массивов и вывести в виде массива
// топорный способ, был придуман с филиппом и доработан позже
// это не очень хорошее решение, потому что есть лучшее в другом файле
// решение оказалось очень медленным, лучше вообще нигде его не использовать...
const twoArrays = (a, b) => {
a.sort((a, b) => a - b);
b.sort((a, b) => a - b);
let currDigit = a[0];
for (let i = 1; i < a.length; i++) {
if (a[i] === currDigit) {
a.splice(i, 1);
i--;
}
else {
currDigit = a[i]
}
}
currDigit = b[0];
for (let i = 1; i < b.length; i++) {
if (b[i] === currDigit) {
b.splice(i, 1);
i--;
}
else {
currDigit = b[i]
}
}
let result = [];
a.forEach((elem) => {
if (b.includes(elem)) result.push(elem)
})
return result
}
console.log(twoArrays([4,9,5,4,4,4,4], [9,4,9,8,4]))