JavaScript Max Element in an Array (original) (raw)
Last Updated : 19 Nov, 2024
These are the following ways to find the largest element in JS array:
**Note: You must add a conditional check for checking whether the given array is empty or not, if the array is not empty then you can use the given below approaches to find out the largest element among them.
1. Using the Spread Operator
The spread operator (...) allows an iterable such as an array to be expanded in places where zero or more arguments or elements are expected. This operator can be used with Math.max() to find the largest element in the array.
**Note: The max() function returns "-infinity" for the empty array.
JavaScript `
const a = [22, 65, 1, 39]; console.log(Math.max(...a));
`
2. Using the filter() Method
The filter() method creates a new array with all elements that pass the test implemented by the provided function. We can use this method to filter out all elements except the largest one.
JavaScript `
const a = [22, 65, 1, 39]; let max = a[0]; a.filter(num => { if (num > max) { max = num; } });
console.log(max);
`
3. U**sing For Loop
- Create a local variable **max and initiate it to **arr[0] to store the maximum among the list
- Initiate an integer **i = 0 and repeat steps 3 to 5 till **i reaches the end of the array.
- Compare arr[i] with **max.
- If arr[i] > max, update **max = arr[i].
- Increment **i once.
- After the iteration is over, return **max as the required answer. JavaScript `
let a = [22, 65, 1, 39]; let i; let max = a[0];
for (i = 1; i < a.length; i++) { if (a[i] > max) max = a[i]; }
console.log(max);
`
4. U**sing Math.max() and apply() Methods
The JavaScript **Math max() Method is used to return the largest of zero or more numbers. The result is “-Infinity” if no arguments are passed and the result is NaN if at least one of the arguments cannot be converted to a number. The apply()
function allows you to pass an array of arguments to the Math.max()
function.
JavaScript `
const a = [22, 65, 1, 39]; console.log( Math.max.apply(null, a));
`
5. U**sing reduce() Method
The Javascript **arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.
JavaScript `
const arr = [22, 65, 1, 39]; const ans = arr.reduce(function (a, b) { return (a > b) ? a : b }); console.log(ans);
`
6. U**sing sort() Method
The Javascript **array.sort() is an inbuilt method in JavaScript that is used to sort the array. An array can be of any type i.e. string, numbers, characters, etc. Here array is the set of values that are going to be sorted.
JavaScript `
const arr = [22, 65, 1, 39]; arr.sort((a, b) => b - a); console.log(arr[0]);
`
7. U**sing Recursion
- Create a recursive function.
- Set an integer i = 0 to denote the current index being searched.
- Return steps 4 to 7 to get the final answer.
- If i is the last index, return arr[i].
- Increment i and call the recursive function for the new value of i.
- Compare the maximum value returned from the recursion function with arr[i].
- Return the max between these two from the current recursion call. JavaScript `
function largest(a, n, i) { if (i == n - 1) { return a[i]; }
// Find the maximum from rest of the array
let recMax = largest(a, n, i + 1);
// Compare with i-th element and return
return Math.max(recMax, a[i]);
}
const a = [22, 65, 1, 39]; const n = a.length; console.log(largest(a, n, 0));
`
8. Using the forEach Method
The forEach method iterates over each element in the array, comparing each element to a variable (max) initialized to the first element. If the current element is greater than max, max is updated. After the loop, max holds the largest value.
JavaScript `
let a = [1, 2, 3, 4, 5]; let max = a[0]; a.forEach(num => { if (num > max) { max = num; } }); console.log(max);
`
Similar Reads
- Learn Data Structures with Javascript | DSA using JavaScript Tutorial JavaScript (JS) is the most popular lightweight, interpreted programming language, and might be your first preference for Client-side as well as Server-side developments. But have you thought about using JavaScript for DSA? Learning Data Structures and Algorithms can be difficult when combined with 7 min read
- Learn Algorithms with Javascript | DSA using JavaScript Tutorial This Algorithms with Javascript tutorial is designed to help you understand and implement fundamental algorithms using the versatile JavaScript programming language. Whether you are a beginner in programming or looking to enhance your algorithmic skills, this guide will walk you through essential co 15+ min read