You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: workspaces/javascript-leetcode-month/problems/2626-array-reduce-transformation/README.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,4 +2,10 @@
2
2
3
3
[View Problem on LeetCode](https://leetcode.com/problems/array-reduce-transformation/)
4
4
5
+
This is yet another problem asking us to re-implement some built-in JavaScript function. Here, it's actually slightly simpler than the built-in -- whereas [`Array.prototype.reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) also passes an index to the reducing function, in this problem we don't care about the index, only the value.
6
+
7
+
We can get a quick accept if we ignore the problem statement's request and use the built-in`.reduce`.
8
+
9
+
For a more serious solution, we can go with any kind of loop over the array values, updating a result during the loop.
10
+
5
11
Once you've worked on the problem, check out [the full write-up and solution](solution.md)!
Copy file name to clipboardExpand all lines: workspaces/javascript-leetcode-month/problems/2626-array-reduce-transformation/solution.md
+31Lines changed: 31 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,12 +7,22 @@
7
7
8
8
## Summary
9
9
10
+
The solution needs to process all the array elements and pass them through a reducing function. Unlike the [`Array.prototype.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) built-in we're replicating, we don't have to pass in an index to the reducer function, but order still matters, since the reducer could be something like `(accumulator, element) => 2 * accumulator + element`.
11
+
12
+
Any approach that processes elements in the appropriate order will work well. I'd recommend an iterative solution using a simple loop, though it's a good idea to also become comfortable with recursive implementations.
13
+
14
+
We can also get accepted by simply using the built-in `.reduce`, since LeetCode doesn't enforce that we don't. Or, we could use [`.reduceRight`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight) instead and claim that it's technically not `.reduce`.
15
+
10
16
## Background
11
17
12
18
## Solutions
13
19
20
+
It's solution time!
21
+
14
22
### Using `Array.prototype.reduce`
15
23
24
+
Let's kick off using the built-ins, for some quick gratification. In pure JavaScript it's very concise:
25
+
16
26
[View Submission on LeetCode](https://leetcode.com/problems/array-reduce-transformation/submissions/1378766665/)
In TypeScript, let's modify LeetCode's solution template and generalize our function using generics. We'll use one type parameter for the type of the array elements and one for the type of the result, as described in more detail in [another write-up](../2635-apply-transform-over-each-element-in-array/solution.md).
39
+
28
40
[View Submission on LeetCode](https://leetcode.com/problems/array-reduce-transformation/submissions/1378765352/)
Understanding why the above works was a bonus question in [another write-up](../2635-apply-transform-over-each-element-in-array/solution.md). Read about it there if you're curious, but otherwise don't worry about this code too much.
65
+
50
66
[View Submission on LeetCode](https://leetcode.com/problems/array-reduce-transformation/submissions/1378777231/)
51
67
68
+
### Using `Array.prototype.reduceRight`
69
+
70
+
For a clearer conscience, we can also go with `.reduceRight`, but we'll have to reverse the array to make sure elements are processed in the appropriate order. The built-in `.reverse` mutates the array it's invoked on, so if we want to avoid mutating the input, we'll have to copy it, using [spread syntax](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) for example.
We can also use the more recently-added [`.toReversed`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed) to get a reversed copy of the input. The function is available in LeetCode's environment, but TypeScript doesn't know that it is, so we'll have to help it out by adding some functions to the core interfaces for `Array` and `ReadonlyArray`.
81
+
60
82
[View Submission on LeetCode](https://leetcode.com/problems/array-reduce-transformation/submissions/1378778477/)
The iterative solutions are the ones I recommend for this problem, because I think they read quite nicely. For example, a simple `for...of` loop works great, since we don't care about indexes:
105
+
82
106
[View Submission on LeetCode](https://leetcode.com/problems/array-reduce-transformation/submissions/1378776182/)
83
107
84
108
```typescript []
@@ -97,6 +121,8 @@ function reduce<TElement, TResult>(
97
121
}
98
122
```
99
123
124
+
Alternatively, try a `.forEach`:
125
+
100
126
[View Submission on LeetCode](https://leetcode.com/problems/array-reduce-transformation/submissions/1378776438/)
101
127
102
128
```typescript []
@@ -115,6 +141,8 @@ function reduce<TElement, TResult>(
115
141
}
116
142
```
117
143
144
+
If we don't mind mutating the input array, we can also remove elements from it, one-by-one, and destructively reduce to a result:
145
+
118
146
[View Submission on LeetCode](https://leetcode.com/problems/array-reduce-transformation/submissions/1378781727/)
119
147
120
148
```typescript []
@@ -134,6 +162,9 @@ function reduce<TElement, TResult>(
134
162
}
135
163
```
136
164
165
+
> [!NOTE]
166
+
> **Why use `.reverse` and `.pop` instead of `.shift` in the destructive implementation?** The answer is at the bottom of this doc.
167
+
137
168
### Recursive
138
169
139
170
[View Submission on LeetCode](https://leetcode.com/problems/array-reduce-transformation/submissions/1378782688/)
Copy file name to clipboardExpand all lines: workspaces/javascript-leetcode-month/problems/2634-filter-elements-from-array/README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
[View Problem on LeetCode](https://leetcode.com/problems/filter-elements-from-array/)
4
4
5
-
This is one of many problems that essentially ask us to implement some built-in JavaScript function. Here, we're asked to implement filtering without using [`.filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter), so of course the first thing we should do is get accepted using `.filter`!
5
+
This is one of several problems that essentially ask us to implement some built-in JavaScript function. Here, we're asked to implement filtering without using [`.filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter), so of course the first thing we should do is get accepted using `.filter`!
6
6
7
7
For a more serious solution, a loop that builds the result works just fine.
Understanding why the above works was a bonus question in [an earlier write-up](../2635-apply-transform-over-each-element-in-array/solution.md). Read about it there if you're curious, but otherwise don't worry about this code too much.
58
+
Understanding why the above works was a bonus question in [another write-up](../2635-apply-transform-over-each-element-in-array/solution.md). Read about it there if you're curious, but otherwise don't worry about this code too much.
0 commit comments