2

Java Program to Find Largest Element in an Array

 2 years ago
source link: https://www.geeksforgeeks.org/videos/java-program-to-find-largest-element-in-an-array/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Java Program to Find Largest Element in an Array

Java Program to Find Largest Element in an Array
  • 110 Views
  • 01/08/2022

In this video, we will see a JAVA program to find the largest element in an array.

In this video we see 5 different approaches for finding the largest element in an array.

1. Using the Iteration method
2. Using Sorting
3. Using Sorting with Array.Sort(A)
4. Using collection framework
5. Using JAVA 8 stream

Examples:
Input : arr[] = {28, 24, 30}
Output: 30

Input : arr[] = {63, 88, 66, 86, 97}
Output: 97

Approach:
Step 1: Create a local variable to store the maximum among the list.
Step 2: Initialize max with the first element initially, to start the comparison.
Step 3: Then traverse the given array from the next element to end, and for each element:
Step 4: Compare the current element with max.
Step 5: If the current element is greater than max, then replace the value of max with the current element.
Step 6: Print the value of the largest element of the array stored in max.

Apart from that, we will see the Time and space complexity of each and every method.

Java Program to find largest element in an array:
https://www.geeksforgeeks.org/java-program-for-program-to-find-largest-element-in-an-array/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK