-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathremoveConsecutive.js
More file actions
36 lines (32 loc) · 958 Bytes
/
Copy pathremoveConsecutive.js
File metadata and controls
36 lines (32 loc) · 958 Bytes
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
const isArray = function (content) {
return Array.isArray(content);
};
const isEqual = function (content1, content2) {
if (content1 === content2) {
return true;
}
if (isArray(content1) && isArray(content2)) {
return areArraysEqual(content1, content2);
}
return false;
};
const areArraysEqual = function (array1, array2) {
if (array1.length !== array2.length) {
return false;
}
for (let index = 0; index < array1.length; index++) {
if (!isEqual(array1[index], array2[index])) {
return false;
}
}
return true;
};
const removeConsecutiveDuplicates = function (list, element) {
if (!isEqual(element, list[list.length - 1])) {
list.push(element);
}
return list;
};
console.log([1, [1, 2], [1, 2], 1, 2, 2, 3].reduce(removeConsecutiveDuplicates, []));
console.log([1, 1, 2, 2, 3].reduce(removeConsecutiveDuplicates, []));
console.log('hello'.split('').reduce(removeConsecutiveDuplicates, []).join(''));