-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path80_remove_duplicates.js
More file actions
39 lines (37 loc) · 920 Bytes
/
80_remove_duplicates.js
File metadata and controls
39 lines (37 loc) · 920 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
37
38
39
const testProblems = [{
input: [1, 1, 1],
output: [1, 1]
}, {
input: [1, 1, 1, 2, 2, 2, 3],
output: [1, 1, 2, 2, 3]
}, {
input: [1, 2, 3, 4, 5, 6, 7],
output: [1, 2, 3, 4, 5, 6, 7]
}, {
input: [1, 1, 1, 1],
output: [1, 1]
}, {
input: [1, 1, 1, 1, 2],
output: [1, 1, 2]
}]
var removeDuplicates = function(nums) {
var indexsToBeRemoved = []
var deleteMode = false
var alreadyDelete = 0
nums.reduce((prev, cur, curIdx) => {
if (prev === cur && deleteMode) {
indexsToBeRemoved.push(curIdx)
} else if (prev === cur) {
deleteMode = true
} else {
deleteMode = false
}
return cur
}, 'x')
indexsToBeRemoved.map((idx, shift) => nums.splice(idx - shift, 1))
return nums.length
};
testProblems.map(problem => {
var answer = removeDuplicates(problem.input)
console.log(`Answer: ${JSON.stringify(answer)} | Expected: ${JSON.stringify(problem.output)}`)
})