DEV Community

Ganesh K S
Ganesh K S

Posted on

Find the largest element within the array

this is my given array

const arr=[5,10,4,24,13,8]
Enter fullscreen mode Exit fullscreen mode
  • *largest is set to first element in the array --- largest = arr[0] *
  • You need to set the first element to largest
  • next need to loop from 1 st index to last index
  • in each loop check if the array value is larger than that in that case largest will be the current iterating element

Image description

Program to find the largest element in the array

let largest = arr[0];

for(let i=1;i<arr.length;i++){
  if(arr[i] > largest){
    largest=arr[i]
  }
}

console.log(largest)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)