🤓5 Diff Method | Solution in TypeScript and JS | Learn JS with Question ✅ | Day-6 ✊🏃
Intuition\nThereduce functionis a higher-order function that takes an array, a reducer function, and an initial value, and returns a single accumulated value by
# Intuition\nThe` reduce function `is a higher-order function that takes an array, a reducer function, and an initial value, and returns a single accumulated value by applying the reducer function to each element of the array.\n\n# Approach\nTo implement the `reduce function`, we can iterate over each element of the array, apply the reducer function to the current value and the current element, and update the accumulated value. We can use a for loop, forEach method, or a for...of loop to perform the iteration.\n\n# Complexity\n- Time complexity:\nThe time complexity of the reduce function implementation is O(n), where n is the length of the array, because the function iterates over each element of the array exactly once. \n\n- Space complexity:\nO(1), as it only uses a single variable to store the accumulated value.\n\n# What We Learn \nBy implementing the reduce function, we learn how to use higher-order functions to transform and reduce data in an array. We also learn how to use different approaches, such as for loops, forEach method, or for...of loops, to iterate over arrays.\n\n# Additional information:\nThe `fn parameter `is a function that takes two arguments: the accumulated value and the current element of the` array.` The purpose of the fn function is to perform a specific operation on these two values and return the result.\n\n# Code In JavaScript\n```\n/**\n * @param {number[]} nums\n * @param {Function} fn\n * @param {number} init\n * @return {number}\n */\nvar reduce = function(nums, fn, init) {\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\n# Code In TypeScript \n```\ntype Reducer<T, U> = (acc: T, curr: U) => T;\n\nfunction reduce<T, U>(nums: U[], fn: Reducer<T, U>, init: T): T {\n  let val: T = init;\n  for (let i = 0; i < nums.length; i++) {\n    val = fn(val, nums[i]);\n  }\n  return val;\n}\n\n```\n# Using forEach loop\n```\n/**\n * @param {number[]} nums\n * @param {Function} fn\n * @param {number} init\n * @return {number}\n */\nvar reduce = function(nums, fn, init) {\n  let val = init;\n  nums.forEach(num => {\n    val = fn(val, num);\n  });\n  return val;\n};\n\n```\n\n# Using reduceRight\n\n```\nfunction reduceArray(nums, fn, init) {\n  return nums.reverse().reduceRight((val, num) => fn(val, num), init);\n}\n\n```\n\n# Using recursion\n```\nfunction reduceArray(nums, fn, init) {\n  if (nums.length === 0) {\n    return init;\n  } else {\n    const head = nums[0];\n    const tail = nums.slice(1);\n    const val = fn(init, head);\n    return reduceArray4(tail, fn, val);\n  }\n}\n\n```\n\n# Using for...of loop\n```\nfunction reduceArray(nums, fn, init) {\n  let val = init;\n  for (const num of nums) {\n    val = fn(val, num);\n  }\n  return val;\n}\n\n```\n\n![upvote-1.png](https://assets.leetcode.com/users/images/85bf72b5-16de-4457-9093-91536e0d5586_1683678139.3167114.png)\n\n
['TypeScript', 'JavaScript']