✔️2626. Array Reduce✔️Level up🚀your JavaScript skills with these intuitive implementations󠁓🚀
Intuition\n Describe your first thoughts on how to solve this problem. \n>The reduce function takes an array of integers, a reducer function, and an initial val
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n>The `reduce` function takes an array of integers, a reducer function, and an initial value as input. It returns a single value that is the result of applying the reducer function to every element in the array. The reducer function takes two parameters - an accumulator and a current value - and returns a new accumulator value. The `reduce` function applies the reducer function to the initial value and the first element in the array to get a new accumulator value, and then applies the reducer function to the new accumulator value and the second element in the array, and so on, until every element in the array has been processed. The final accumulator value is returned as the result.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n>We start by checking if the input array is empty. If it is, we simply return the initial value.\n\n>If the input array is not empty, we initialize the accumulator to the initial value. We then loop through the array, applying the reducer function to the accumulator and the current element in each iteration. The result of each iteration becomes the new accumulator value for the next iteration.\n\n>After looping through all the elements in the array, we return the final accumulator value.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n>The time complexity of the `reduce` function is O(n), where n is the length of the input array. This is because we need to loop through every element in the array and apply the reducer function to it.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n>The space complexity of the `reduce` function is O(1), because we only use a constant amount of additional memory to store the accumulator and the loop index variable. The space required by the input array and the reducer function is not counted as additional memory usage by the `reduce` function.\n\n# Code\n``` JS []\n/**\n * @param {number[]} nums\n * @param {Function} fn\n * @param {number} init\n * @return {number}\n */\nvar reduce = function(nums, fn, init) {\n    if (nums.length === 0) {\n        return init;\n    }\n    let val = init;\n    for (let i = 0; i < nums.length; i++) {\n        val = fn(val, nums[i]);\n    }\n    return val;\n};\n```\n```TS []\nfunction reduce(nums: number[], fn: Function, init: number): number {\n  if (nums.length === 0) {\n    return init;\n  }\n  let val: number = init;\n  for (let i = 0; i < nums.length; i++) {\n    val = fn(val, nums[i]);\n  }\n  return val;\n}\n\n```\n![image.png](https://assets.leetcode.com/users/images/b427e686-2e5d-469a-8e7a-db5140022a6b_1677715904.0948765.png)\n# Please Upvote\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution.\uD83D\uDE0A Keep Learning\nPlease give my solution an upvote! \uD83D\uDC4D\nIt\'s a simple way to show your appreciation and\nkeep me motivated. Thank you! \uD83D\uDE0A\n```\n> There are multiple ways to implement the reduce function. Here are a few alternatives:\n\n\n1. Using the `forEach` method:\n```\nfunction reduce(nums, fn, init) {\n  let val = init;\n  nums.forEach(num => val = fn(val, num));\n  return val;\n}\n\n```\n2. Using the `reduceRight` method:\n```\nfunction reduce(nums, fn, init) {\n  return nums.reduceRight(fn, init);\n}\n\n```\n3. Using a `for...of` loop:\n```\nfunction filter(arr, fn) {\n  return Array.from(arr, (val, index) => fn(val, index) && val);\n}\n```\n4. Using recursion:\n```\nfunction reduce(nums, fn, init) {\n  if (nums.length === 0) {\n    return init;\n  } else if (nums.length === 1) {\n    return fn(init, nums[0]);\n  } else {\n    const mid = Math.floor(nums.length / 2);\n    const left = nums.slice(0, mid);\n    const right = nums.slice(mid);\n    return fn(reduce(left, fn, init), reduce(right, fn, init));\n  }\n}\n\n```\n5. Using `Array.prototype.reduce.call`:\n```\nfunction reduce(nums, fn, init) {\n  return Array.prototype.reduce.call(nums, fn, init);\n}\n```\n\n6. Using the `Array.from` method:\n```\nfunction reduce(nums, fn, init) {\n  return Array.from(nums).reduce(fn, init);\n}\n\n```\n# Important topic to Learn\n\n| Sr No. | Topic |\n|-----|-----|\n1.|Array methods|\n2.|Functional programming|\n3.|Higher-order functions|\n\n\n# Please Comment\uD83D\uDC4D\uD83D\uDC4D\n```\nThanks for visiting my solution comment below if you like it.\uD83D\uDE0A\n```\n\n\n\n\n\n\n\n\n\n\n\n\n
['TypeScript', 'JavaScript']