Array Reduce Transformation
Problem
Given an integer array nums, a reducer function fn, and an initial value init, return the final result obtained by executing the fn function on each element of the array, sequentially, passing in the return value from the calculation on the preceding element.
This result is achieved through the following operations: val = fn(init, nums[0]), val = fn(val, nums[1]), val = fn(val, nums[2]), ... until every element in the array has been processed. The ultimate value of val is then returned.
If the length of the array is 0, the function should return init.
Please solve it without using the built-in Array.reduce method.
Examples
Example 1
Input:
nums = [1,2,3,4]
fn = function sum(accum, curr) { return accum + curr; }
init = 0
Output: 10
Explanation:
initially, the value is init=0.
(0) + nums[0] = 1
(1) + nums[1] = 3
(3) + nums[2] = 6
(6) + nums[3] = 10
The final answer is 10.
Example 2
Input:
nums = [1,2,3,4]
fn = function sum(accum, curr) { return accum + curr * curr; }
init = 100
Output: 130
Explanation:
initially, the value is init=100.
(100) + nums[0] * nums[0] = 101
(101) + nums[1] * nums[1] = 105
(105) + nums[2] * nums[2] = 114
(114) + nums[3] * nums[3] = 130
The final answer is 130.
Example 3
Input:
nums = []
fn = function sum(accum, curr) { return 0; }
init = 25
Output: 25
Explanation: For empty arrays, the answer is always init.
Constraints
0 <= nums.length <= 10000 <= nums[i] <= 10000 <= init <= 1000
Solution
Method 1 - Simple For-each Loop
Intuition
Simulate the reduce operation by iteratively applying the reducer function to the accumulator and each element, starting from the initial value.
Approach
Define a function that takes nums, fn, and init. Start with ans = init, then for each element, update ans = fn(ans, num). Return ans at the end. If nums is empty, return init immediately.
Code
JavaScript
function reduce(nums, fn, init) {
let ans = init;
for (const num of nums) {
ans = fn(ans, num);
}
return ans;
}
Python
from typing import List, Callable
def reduce(nums: List[int], fn: Callable[[int, int], int], init: int) -> int:
ans = init
for num in nums:
ans = fn(ans, num)
return ans
Complexity
- ⏰ Time complexity:
O(n) - 🧺 Space complexity:
O(1)
Method 2 - Using Array.forEach
Code
JS
var reduce = function(nums, fn, init) {
let acc = init;
nums.forEach((element) => {
acc = fn(acc, element);
});
return acc;
};
TS
type Fn = (accum: number, curr: number) => number
function reduce(nums: number[], fn: Fn, init: number): number {
let acc = init;
nums.forEach((element: number) => {
acc = fn(acc, element);
});
return acc;
};
Python
from typing import List, Callable
def reduce(nums: List[int], fn: Callable[[int, int], int], init: int) -> int:
acc = init
for num in nums:
acc = fn(acc, num)
return acc
Method 3 - Using array.reduce
This method employs the Array.reduce() function to iterate through each array element, progressively updating a cumulative value.
Code
JS
var reduce = function(nums, fn, init) {
return nums.reduce((acc, element) => fn(acc, element), init);
};
Python
from functools import reduce as builtin_reduce
from typing import List, Callable
def reduce(nums: List[int], fn: Callable[[int, int], int], init: int) -> int:
return builtin_reduce(fn, nums, init)
Method 4 - Python Using List Comprehension with Walrus Operator
Code
Python
from typing import List, Callable
def reduce(nums: List[int], fn: Callable[[int, int], int], init: int) -> int:
acc = init
[acc := fn(acc, num) for num in nums]
return acc