Skip to main content

Java Program to find Largest Number in an Array

We can find the largest number in an array in java by sorting the array and returning the largest number. Let's see the full example to find the largest number in java array.


package com.knf;

public class LargestInArrayExample1 {

public static void main(String args[]) {
int a[] = { 2, 1, 2, 5, 6, 3, 2, 9 };
int b[] = { 144, 666, 99, 677, 433, 422, 255 };
System.out.println("Largest: " + getLargest(a));
System.out.println("Largest: " + getLargest(b));
}

public static int getLargest(int[] a) {
int temp;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[a.length - 1];
}
}

Output:

Largest: 9
Largest: 677

Example 2 :Using Arrays


Let's see another example to get largest element in java array using Arrays.

package com.knf;

import java.util.Arrays;

public class LargestInArrayExample2 {

public static void main(String args[]) {
int a[] = { 2, 1, 2, 5, 6, 3, 2, 9 };
int b[] = { 144, 666, 99, 677, 433, 422, 255 };
System.out.println("Largest: " + getLargest(a));
System.out.println("Largest: " + getLargest(b));
}

public static int getLargest(int[] a) {
Arrays.sort(a);
return a[a.length - 1];

}
}

Output:

Largest: 9
Largest: 677

Example 3 :Using Collections


Let's see another example to get largest number in java array using collections.

package com.knf;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class LargestInArrayExample3 {

public static void main(String args[]) {
Integer a[] = { 2, 1, 2, 5, 6, 3, 2, 9 };
Integer b[] = { 144, 666, 99, 677, 433, 422, 255 };
System.out.println("Largest: " + getLargest(a));
System.out.println("Largest: " + getLargest(b));
}

public static int getLargest(Integer[] a) {
List<Integer> list=Arrays.asList(a);
Collections.sort(list);
return a[a.length - 1];

}
}

Output:

Largest: 9
Largest: 677


This article is contributed by Sibin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Comments

Popular posts from this blog

Spring boot video streaming example-HTML5

Hello everyone, Today we will learn how to stream MP4 video using Spring. You can download the source code of this sample application from our GitHub repository. Download Link is provided at the end of this post.
User Interface

Project Structure


Home Controller
@Controller
public class HomeController {
 @Autowired
 private MyResourceHttpRequestHandler handler;
 private final static File MP4_FILE = new File("D:\\videofiles\\video1.mp4");
 // supports byte-range requests
 @GetMapping("/index")
 public String home() {
  return "index";
 }
 // supports byte-range requests
 @GetMapping("/byterange")
 public void byterange( HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setAttribute(MyResourceHttpRequestHandler.ATTR_FILE, MP4_FILE);
  handler.handleRequest(request, response);
 }}
Spring Boot 
@SpringBootApplication
@ComponentScan({ "com" })
public class KnowledgefactorydemoApplication {
 public static vo…

DataTable-Pagination example with Spring boot,jQuery and ajax free download

This article shows you DataTable-Pagination example with Spring boot, jQuery and ajax. 

Following technologies stack being used:
Spring Boot 2.1.1.RELEASESpring 5.1.3.RELEASE Maven 3DatatablejQueryFree Markerjavascriptajax Project Structure
View(index.ftl) <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Knf|Pagination-Datatables</title> <!-- Tell the browser to be responsive to screen width --> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="../../css/jquery.dataTables.min.css"> <body>  <section class="content">   <div class="container-fluid">    <table id="example" class="display" style="width: 100%">     <thead>      <tr>       <th>First name</th>       <th>La…

9 Best Job Posting Sites for Employee 2020-2022

One of the most common ways today’s job seekers uncover employment opportunities are by using online sources. There are hundreds of job boards, both generic and niche, as well as aggregators, social media channels, networking groups, and staffing company websites to choose from. The good news is they provide plenty of listings. The bad news is you can be easily overwhelmed by so many options. Here, knowledge factory offers 9 of what is generally considered the best job search websites around. There are many others, of course, but this list of best job search websites should give you a good starting point for your next job hunt: 1. LinkedIn
This top networking site allows you to find jobs through your extended network. User profiles serve as resumes. You can easily share career-related content and solicit or supply recommendations. Additionally, you can join groups, participate in conversations, and follow companies you find interesting and relevant to your job search. Premium pai…