Answers for "find max element in array javascript without inbuilt method"

30

max value in array javascript

// For large data, it's better to use reduce. Supose arr has a large data in this case:
const arr = [1, 5, 3, 5, 2];
const max = arr.reduce((a, b) => { return Math.max(a, b) });

// For arrays with relatively few elements you can use apply: 
const max = Math.max.apply(null, arr);

// or spread operator:
const max = Math.max(...arr);
Posted by: Guest on November-27-2020
0

js find all max number indexes in array

const arr = [0,1,4,3,4];
const max = Math.max(...arr);
let res = [];
arr.forEach((item, index) => item === max ? res.push(index): null);
console.log(res);
Posted by: Guest on December-30-2021

Code answers related to "find max element in array javascript without inbuilt method"

Code answers related to "Javascript"

Browse Popular Code Answers by Language