Skip to content

Commit 2c53d2d

Browse files
authored
Adding mutating input Solution for 2635-apply-transform-over-each-element-in-array (#408)
Added another solution for this problem 😄
1 parent 38a079a commit 2c53d2d

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

workspaces/javascript-leetcode-month/problems/2635-apply-transform-over-each-element-in-array/solution.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,20 @@ function map<TIn, TOut>(
454454
}
455455
```
456456

457+
Also, since the output is the same size as the input, you could modify the array to save space:
458+
459+
[View submission on LeetCode](https://leetcode.com/problems/apply-transform-over-each-element-in-array/submissions/1380674098/)
460+
461+
```typescript []
462+
function map<TIn>(arr: TIn[], fn: (element: TIn, index: number) => TIn): TIn[] {
463+
for (let i = 0; i < arr.length; ++i) {
464+
arr[i] = fn(arr[i], i);
465+
}
466+
467+
return arr;
468+
}
469+
```
470+
457471
## Answers to Bonus Questions
458472

459473
1. **What is the significance of the `void`s in `Generator<number, void, void>`?**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function map<TIn>(arr: TIn[], fn: (element: TIn, index: number) => TIn): TIn[] {
2+
for (let i = 0; i < arr.length; ++i) {
3+
arr[i] = fn(arr[i], i);
4+
}
5+
6+
return arr;
7+
}

0 commit comments

Comments
 (0)