Write a Java Program to Find Max From the Given Array
Program to find largest element in an array
Given an array, find the largest element in it.
Example:
Input : arr[] = {10, 20, 4} Output : 20 Input : arr[] = {20, 10, 20, 4, 100} Output : 100 Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out our self-paced courses designed for students of grades I-XII.
Start with topics like Python, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, because GeeksforGeeks School is now here!
The solution is to initialize max as first element, then traverse the given array from second element till end. For every traversed element, compare it with max, if it is greater than max, then update max.
C++
#include <bits/stdc++.h>
using namespace std;
int largest( int arr[], int n)
{
int i;
int max = arr[0];
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
int main()
{
int arr[] = {10, 324, 45, 90, 9808};
int n = sizeof (arr) / sizeof (arr[0]);
cout << "Largest in given array is "
<< largest(arr, n);
return 0;
}
C
#include <stdio.h>
int largest( int arr[], int n)
{
int i;
int max = arr[0];
for (i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
int main()
{
int arr[] = {10, 324, 45, 90, 9808};
int n = sizeof (arr)/ sizeof (arr[0]);
printf ( "Largest in given array is %d" , largest(arr, n));
return 0;
}
Java
class Test
{
static int arr[] = { 10 , 324 , 45 , 90 , 9808 };
static int largest()
{
int i;
int max = arr[ 0 ];
for (i = 1 ; i < arr.length; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
public static void main(String[] args)
{
System.out.println( "Largest in given array is " + largest());
}
}
Python3
def largest(arr,n):
return ( max (arr))
arr = [ 10 , 324 , 45 , 90 , 9808 ]
n = len (arr)
Ans = largest(arr,n)
print ( "Largest in given array is" ,Ans)
C#
using System;
class GFG {
static int []arr = {10, 324, 45, 90, 9808};
static int largest()
{
int i;
int max = arr[0];
for (i = 1; i < arr.Length; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
public static void Main()
{
Console.WriteLine( "Largest in given "
+ "array is " + largest());
}
}
PHP
<?php
function largest( $arr , $n )
{
$i ;
$max = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
if ( $arr [ $i ] > $max )
$max = $arr [ $i ];
return $max ;
}
$arr = array (10, 324, 45, 90, 9808);
$n = sizeof( $arr );
echo "Largest in given array is "
, largest( $arr , $n );
?>
Javascript
<script>
function largest(arr) {
let i;
let max = arr[0];
for (i = 1; i < arr.length; i++) {
if (arr[i] > max)
max = arr[i];
}
return max;
}
let arr = [10, 324, 45, 90, 9808];
document.write( "Largest in given array is " + largest(arr));
</script>
Output:
Largest in given array is 9808
Using Library Function :
We use std::max_element in C++.
C++
#include <bits/stdc++.h>
using namespace std;
int largest( int arr[], int n)
{
return *max_element(arr, arr+n);
}
int main()
{
int arr[] = {10, 324, 45, 90, 9808};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << largest(arr, n);
return 0;
}
Java
import java .io.*;
import java.util.*;
class GFG
{
static int largest( int []arr,
int n)
{
Arrays.sort(arr);
return arr[n - 1 ];
}
static public void main (String[] args)
{
int []arr = { 10 , 324 , 45 ,
90 , 9808 };
int n = arr.length;
System.out.println(largest(arr, n));
}
}
Python3
def largest(arr, n):
return max (arr)
arr = [ 10 , 324 , 45 , 90 , 9808 ]
n = len (arr)
print (largest(arr, n))
C#
using System;
using System.Linq;
public class GFG {
static int largest( int []arr, int n)
{
return arr.Max();
}
static public void Main ()
{
int []arr = {10, 324, 45, 90, 9808};
int n = arr.Length;
Console.WriteLine( largest(arr, n));
}
}
PHP
<?php
function largest( $arr , $n )
{
return max( $arr );
}
$arr = array (10, 324, 45, 90, 9808);
$n = count ( $arr );
echo largest( $arr , $n );
?>
Javascript
<script>
function largest(arr, n)
{
arr.sort();
return arr[n-1];
}
let arr = [10, 324, 45, 90, 9808];
let n = arr.length;
document.write(largest(arr, n));
</script>
Output :
9808
Time complexity of the above solution is
.
Refer below article for more methods.
Program to find the minimum (or maximum) element of an array
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Write a Java Program to Find Max From the Given Array
Source: https://www.geeksforgeeks.org/c-program-find-largest-element-array/
0 Response to "Write a Java Program to Find Max From the Given Array"
Post a Comment