diff --git a/workspaces/javascript-leetcode-month/problems/2635-apply-transform-over-each-element-in-array/solution.md b/workspaces/javascript-leetcode-month/problems/2635-apply-transform-over-each-element-in-array/solution.md index 5b34aa3b..bccede39 100644 --- a/workspaces/javascript-leetcode-month/problems/2635-apply-transform-over-each-element-in-array/solution.md +++ b/workspaces/javascript-leetcode-month/problems/2635-apply-transform-over-each-element-in-array/solution.md @@ -454,6 +454,20 @@ function map( } ``` +Also, since the output is the same size as the input, you could modify the array to save space: + +[View submission on LeetCode](https://leetcode.com/problems/apply-transform-over-each-element-in-array/submissions/1380674098/) + +```typescript [] +function map(arr: TIn[], fn: (element: TIn, index: number) => TIn): TIn[] { + for (let i = 0; i < arr.length; ++i) { + arr[i] = fn(arr[i], i); + } + + return arr; +} +``` + ## Answers to Bonus Questions 1. **What is the significance of the `void`s in `Generator`?** diff --git a/workspaces/javascript-leetcode-month/problems/2635-apply-transform-over-each-element-in-array/solutions/iterative-loops/using-classic-for-mutating-input.ts b/workspaces/javascript-leetcode-month/problems/2635-apply-transform-over-each-element-in-array/solutions/iterative-loops/using-classic-for-mutating-input.ts new file mode 100644 index 00000000..9fbdaf2f --- /dev/null +++ b/workspaces/javascript-leetcode-month/problems/2635-apply-transform-over-each-element-in-array/solutions/iterative-loops/using-classic-for-mutating-input.ts @@ -0,0 +1,7 @@ +function map(arr: TIn[], fn: (element: TIn, index: number) => TIn): TIn[] { + for (let i = 0; i < arr.length; ++i) { + arr[i] = fn(arr[i], i); + } + + return arr; +}