-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
delete-occurrences-of-an-element-if-it-occurs-more-than-n-times.js
108 lines (95 loc) · 2.81 KB
/
delete-occurrences-of-an-element-if-it-occurs-more-than-n-times.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
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
function deleteNth(arr, n){
// a place to keep track of the numbers we have seen (including count)
const counts = {};
// a place to store the result - initialized to empty
const result = [];
// iterate over arr
for (let i = 0; i < arr.length; i++) {
const currentNumber = arr[i];
// if we have not seen the current number n times
if (!counts[currentNumber] || counts[currentNumber] < n) {
// push into result array
result.push(currentNumber);
// increase count
counts[currentNumber] = counts[currentNumber] || 0;
counts[currentNumber]++;
}
}
// return result array
return result;
}
function deleteNth(arr, n){
const counts = {};
const result = [];
arr.forEach(currentNumber => {
if (!counts[currentNumber] || counts[currentNumber] < n) {
result.push(currentNumber);
counts[currentNumber] = counts[currentNumber] || 0;
counts[currentNumber]++;
}
});
return result;
}
function deleteNth(arr, n){
const counts = {};
return arr.filter(currentNumber => {
if (!counts[currentNumber] || counts[currentNumber] < n) {
counts[currentNumber] = (counts[currentNumber] || 0) + 1;
return true;
}
return false;
});
}
function deleteNth(arr, n){
const counts = {};
return arr.filter(currentNumber => {
if (!counts[currentNumber] || counts[currentNumber] < n) {
counts[currentNumber] = (counts[currentNumber] || 0) + 1;
return true;
}
return false;
});
}
function deleteNth(arr, n){
return arr.reduce(({ counts, result }, currentNumber) => {
if (!counts[currentNumber] || counts[currentNumber] < n) {
counts[currentNumber] = (counts[currentNumber] || 0) + 1;
result.push(currentNumber);
}
return {
counts,
result
};
}, {
counts: {},
result: []
}).result;
}
function deleteNth(arr, n){
return arr.reduce((state, currentNumber) => {
if (!state.counts[currentNumber] || state.counts[currentNumber] < n) {
state.counts[currentNumber] = (state.counts[currentNumber] || 0) + 1;
state.result.push(currentNumber);
}
return state;
}, {
counts: {},
result: []
}).result;
}
// Thanks dan levy!
function deleteNth(array, limit) {
return array.reduce((state, item) => {
if (!state.counts[item]) state.counts[item] = 0;
state.counts[item] ++;
if (state.counts[item] <= limit) {
state.array = state.array.concat([item]);
}
return state;
}, {counts: {}, array: []})
}
// Thanks alca!
let deleteNth = (arr, t = 1) => arr.reduce((p, n) => (p[0][n] = (p[0][n] || 0) + 1, p[0][n] <= t && p[1].push(n), p), [ {}, [] ])[1];
console.log(deleteNth([1, 1, 1, 1], 2), [1, 1]);
console.log(deleteNth([20, 37, 20, 21], 1), [20, 37, 21]);
console.log(deleteNth([1, 1, 3, 3, 7, 2, 2, 2, 2], 3), [1, 1, 3, 3, 7, 2, 2, 2]);