Recursive & Iterative Approach | Detailed Explanation
\u2705\u2705 Please Upvote if you find it useful \u2705\u2705\n\n## Recursive Approach:\n\n1. The function flat takes an input array arr and a depth    as param
## \u2705\u2705 Please Upvote if you find it useful \u2705\u2705\n\n## Recursive Approach:\n\n1. The function `flat` takes an input array `arr` and a depth    as parameters.\n2. If the current `depth` equals `0`, the function reaches the base case where no further flattening is required. In this case, the function creates a shallow copy of the array using the `slice()` method and returns it as the result.\n3. If the current depth is greater than `0`, the function proceeds with flattening the array.\n4. The function initializes an empty array called `flattened` to store the flattened elements.\n5. It iterates through each element of the input array using a `for` loop.\n6. For each element, it checks if it is an array using` Array.isArray(arr[i])`.\n7. If the element is an array, it recursively calls the `flat` function with the nested array `(arr[i])` and a decreased depth `(depth - 1)`.\n8. The result of the recursive call is stored in a variable called `nested`.\n9. The `...` spread operator is used to concatenate the elements of the `nested` array into the `flattened` array.\n10. If the element is not an array, it is a single value, so it is directly pushed into the `flattened` array.\n11. After iterating through all the elements, the function returns the `flattened` array as the final result.\n\n# Code\n```\n// Recursive Approach\nfunction flat(arr, depth) {\n  if (depth === 0) {\n    return arr.slice(); // Base case: return a shallow copy of the array\n  }\n\n  let flattened = [];\n\n  for (let i = 0; i < arr.length; i++) {\n    if (Array.isArray(arr[i])) {\n      const nested = flat(arr[i], depth - 1); // Recursively flatten nested arrays\n      flattened.push(...nested); // Concatenate flattened nested arrays to the result\n    } else {\n      flattened.push(arr[i]); // Push individual elements to the result\n    }\n  }\n\n  return flattened; // Return the flattened array\n}\n\n```\n----\n\n## Iterative Approach: \n\nThe function `flat` takes an array `arr` and a depth `depth` as parameters like before.\n\nwe will use a `while` loop to continue the flattening operation as long as there are nested array elements (i.e. `hasNestedArray` is `true`) and the current depth is less than the specified depth.\n\nWithin each iteration of the loop, we initialize a `queue` array to store the flattened elements and then iterates through each element of the input array `arr`.\n\nFor each element, it checks if it\'s an array using `Array.isArray(arr[i])`. If the element is an array, it spreads its elements into the queue array using the spread operator `(...)`. This adds the nested elements to the `queue` for further flattening. Additionally, it sets the `hasNestedArray` flag to true to indicate the presence of nested array elements.\n\nIf the element is not an array, it is a single value, so it is directly pushed into the `queue`.\n\nAfter iterating through all the elements of the current level, the function replaces the original array `arr` with the elements in the `queue`. This prepares for the next iteration of the loop, where the process continues to flatten nested arrays.\n\nwe increment the `currentDepth` at each iteration to keep track of the current depth of nesting.\n\nOnce there are no more nested array elements or the current depth reaches the specified depth, we exit.\n\n```java\n// iterative approach\nfunction flat (arr, depth) {\n  let hasNestedArray = true;\n  let queue;\n  let currentDepth = 0;\n\n  // Continue flattening while there are nested array elements and the current depth is less than the specified depth\n  while (hasNestedArray && currentDepth < depth) {\n    hasNestedArray = false;\n    queue = [];\n\n    // Iterate through each element in the array\n    for (let i = 0; i < arr.length; i++) {\n      if (Array.isArray(arr[i])) {\n        queue.push(...arr[i]); // If the element is an array, spread its elements into the queue\n        hasNestedArray = true; // Set the flag to indicate the presence of nested array elements\n      } else {\n        queue.push(arr[i]); // If the element is not an array, push it into the queue\n      }\n    }\n\n    arr = [...queue]; // Replace the original array with the elements in the queue\n    currentDepth++; // Increment the depth counter\n  }\n\n  return arr; // Return the flattened array\n};\n```\n\n## \u2705\u2705 Please Upvote if you find it useful \u2705\u2705
['JavaScript']