Find largest element in array - DSA in JavaScript

Question:

Solution:

// edge case
if (arr.length === 0) return;

// logic
let max = arr[0];
for (let i = 1; i < arr.length; i++) {
    let current = arr[i];
    if (current > max) {
        max = current;
    }
}
return max;
0
Subscribe to my newsletter

Read articles from Bishal Kumar Shaw directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Bishal Kumar Shaw
Bishal Kumar Shaw