Detecting the Largest Element in an Array of Numbers (nested) in JavaScript Last Updated : 15 May, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a nested array of numbers, we need to find and return the largest element present in the array. Below are the approaches to detecting the largest element in an array of Numbers (nested) in JavaScript: Table of Content Iterative ApproachRecursive ApproachIterative ApproachUse a nested loop to traverse through each element of the array and its subarrays.Keep track of the largest element found so far and initialize it to the smallest possible value.During traversal, if an element is larger than the current largest, update the largest element.Continue traversing until all elements in the nested array have been visited.Return the largest element found after completing the traversal. JavaScript const nestedArray = [1, [2, 3, [4, 5], 6], [7, 8], 9]; function maxIterative(arr) { let largest = Number.MIN_SAFE_INTEGER; for (let i = 0; i < arr.length; i++) { if (Array.isArray(arr[i])) { largest = Math.max(largest, maxIterative(arr[i])); } else { largest = Math.max(largest, arr[i]); } } return largest; } console.log(maxIterative(nestedArray)); Output9 Time Complexity: Time complexity of O(n), where n is the total number of elements in the nested array. Space Complexity: The space complexity is also O(n). Recursive ApproachRecursively traverse through each element of the array and its subarrays.Initialize a variable to keep track of the largest element found so far and set it to the smallest possible value.At each step of recursion, compare the current element with the largest element found so far.If the current element is larger, update the largest element accordingly.Continue the recursion until all elements in the nested array have been visited.Return the largest element found after completing the recursion. JavaScript const nestedArray = [1, [2, 3, [4, 5], 6], [7, 8], 9]; function largestRecursive(arr) { let largest = Number.MIN_SAFE_INTEGER; arr.forEach(element => { if (Array.isArray(element)) { largest = Math.max(largest, largestRecursive(element)); } else { largest = Math.max(largest, element); } }); return largest; } console.log(largestRecursive(nestedArray)); Output9 Time Complexity: Time complexity of O(n), where n is the total number of elements in the nested array. Space Complexity: The space complexity is also O(n). ConclusionThe purpose of these methods is to discover the largest element in an embedded array by either iterating or recursively pursuing the individual element and update the largest element as long as are found elements that are larger than the found one so far. The iterative or recursive approach often is chosen by the programmer's personal preference unlike the other one, as well as the specification of the problem to be solved. Comment More infoAdvertise with us Next Article JavaScript Program to Find N Largest Elements from a Linked List A amit7822i5n Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript Program to find the Longest Consecutive Sequence of Numbers in an Array In this article, we are going to find the longest consecutive sequence of numbers in an array using JavaScript language. We are given an array that consists of integers we need to find the longest subsequence such that elements in the subsequence are consecutive integers, these numbers are in any or 4 min read JavaScript Program to Find Largest Number in a Linked List Finding the largest number in a linked list is a common problem and can be efficiently solved using both iterative and recursive approaches. A linked list is a fundamental data structure where each element, known as a node, is connected to the next one, forming a chain. Table of Content Iterative Ap 3 min read JavaScript Program to Find N Largest Elements from a Linked List This JavaScript program is designed to identify the N largest elements from a linked list. A linked list is a linear data structure where elements, known as nodes, are connected via pointers. The program iterates through the linked list to determine the N largest elements it contains. Table of Conte 4 min read Kth Largest/Smallest Element in Binary Search Tree using JavaScript A binary search tree is a type of binary tree in which each node has two children: left child & right child. In a Binary search tree (BST) all nodes in the left subtree have values less than the node value and all nodes in the right subtree have values greater than the node value. Different appr 4 min read JavaScript - Most Frequent Element in an Array Let us talk about different methods to find the most frequent element in an array in JavaScript.Using JavaScript Object (For Small to Moderate Arrays)In this approach we use JavaScript object to store the number and their occurrences and find the element occurred most frequently.JavaScriptfunction m 3 min read Remove Least Elements to Convert Array to Increasing Sequence using JavaScript Given an array of numbers, our task is to find the minimum number of elements that need to be removed from the array so that the remaining elements form an increasing sequence. Examples: Input: arr = [5, 100, 6, 7, 100, 9, 8];Output: No of elements removed =3, Array after removing elements =[ 5, 6 , 3 min read Like