Java – Find Largest number in an Arrays or List ?

In this article, we will discuss how to find largest number in an Arrays and List

1. Finding Largest number in an Arrays

We will follow below 3 approaches to get Largest number in an Arrays

  • Using SortedSet and TreeSet
  • Using Arrays sorting
  • Using Iterative approach

1.1 Using SortedSet & TreeSet approach

  • Create TreeSet object with SortedSet reference and pass actual Arrays (as List after converting) as constructor-argument to be sorted
  • Now, elements inside TreeSet object will be sorted according to natural-order and last element will be the largest element
  • Using last() method of SortedSet reference, we can get last element and that will be the largest element

FindLargestNumberInAnArraysUsingTreeSet.java

package in.bench.resources.largest.number;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;

public class FindLargestNumberInAnArraysUsingTreeSet {

	public static void main(String[] args) {

		// random numbers
		Integer[] numbers = {5, 9, 11, 2, 8, 21, 1};


		// print to console
		System.out.println("Original Integer Arrays - " 
				+ Arrays.toString(numbers));


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// sort Integer[] arrays using TreeSet - stores in ASC order
		SortedSet<Integer> sortedSet = new TreeSet<Integer>(
				Arrays.asList(numbers) // convert arrays to List
				);


		// this will be largest number in an Arrays
		int largestNumber = sortedSet.last();


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("\nLargest number in an Arrays is - "
				+ largestNumber);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");
	}
}

Output:

Original Integer Arrays - [5, 9, 11, 2, 8, 21, 1]

Largest number in an Arrays is - 21

Execution time - 0 ns

1.2 Using Arrays sorting approach

FindLargestNumberInAnArraysUsingSortMethod.java

package in.bench.resources.largest.number;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;

public class FindLargestNumberInAnArraysUsingSortMethod {

	public static void main(String[] args) {

		// random numbers
		Integer[] numbers = {5, 9, 11, 2, 8, 21, 1};


		// print to console
		System.out.println("Original Integer Arrays - " 
				+ Arrays.toString(numbers));


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// sort Arrays element in ascending order
		Arrays.sort(numbers);


		// last element will be second largest number in an Arrays
		int largestNumber = numbers[numbers.length - 1];


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("\nLargest number in an Arrays is - "
				+ largestNumber);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");
	}
}

Output:

Original Integer Arrays - [5, 9, 11, 2, 8, 21, 1]

Largest number in an Arrays is - 21

Execution time - 0 ns

1.3 Using Iterative approach

  • First assume 1st element of an Arrays is the largest number
  • Then start iterating Arrays one-by-one using for-loop and compare assumed largest number with other iterating numbers and accordingly set largest number

FindLargestNumberInAnArraysUsingIterativeApproach.java

package in.bench.resources.largest.number;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;

public class FindLargestNumberInAnArraysUsingIterativeApproach {

	public static void main(String[] args) {

		// random numbers
		Integer[] numbers = {5, 9, 11, 2, 8, 21, 1};


		// print to console
		System.out.println("Original Integer Arrays - " 
				+ Arrays.toString(numbers));


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// assume largest number
		int max = numbers[0];


		// iterate and find largest number
		for(int index = 0; index < numbers.length; index++) {

			if(numbers[index] > max) {
				max = numbers[index];
			}
		}


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("\nLargest number in an Arrays is - "
				+ max);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");
	}
}

Output:

Original Integer Arrays - [5, 9, 11, 2, 8, 21, 1]

Largest number in an Arrays is - 21

Execution time - 0 ns

2. Finding Largest number in List

We will follow below 2 approaches to get Largest number in a List or ArrayList

  • Using Collection sorting
  • Using Iterative approach

2.1 Using Collection sorting approach

FindLargestNumberInListUsingCollectionsSortMethod.java

package in.bench.resources.largest.number;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class FindLargestNumberInListUsingCollectionsSortMethod {

	public static void main(String[] args) {

		// random numbers
		List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);


		// print to console
		System.out.println("Original Integer List - " 
				+ numbers);


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// sort List element in ascending order
		Collections.sort(numbers);


		// last element will be largest number in List
		int largestNumber = numbers.get(numbers.size() - 1);


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("\nLargest number in List is - "
				+ largestNumber);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");
	}
}

Output:

Original Integer List - [5, 9, 11, 2, 8, 21, 1]

Largest number in List is - 21

Execution time - 0 ns

2.2 Using Iterative approach

  • First assume 1st element of List is the largest number
  • Then start iterating List one-by-one using for-loop and compare assumed largest number with other iterating numbers and accordingly set largest number

FindLargestNumberInListUsingIterativeApproach.java

package in.bench.resources.largest.number;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;

public class FindLargestNumberInListUsingIterativeApproach {

	public static void main(String[] args) {

		// random numbers
		List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);


		// print to console
		System.out.println("Original Integer List - " 
				+ numbers);


		// Execution - start time
		LocalTime startTime = LocalTime.now();


		// assume largest number
		int max = numbers.get(0);


		// iterate and find largest number
		for(int index = 0; index < numbers.size(); index++) {

			if(numbers.get(index) > max) {
				max = numbers.get(index);
			}
		}


		// Execution - end time
		LocalTime endTime = LocalTime.now();


		// find difference
		Duration duration = Duration.between(startTime, endTime);
		long differenceInNano = duration.getNano();


		// print sum to console
		System.out.println("\nLargest number in List is - "
				+ max);


		// print execution time in Nano seconds
		System.out.println("\nExecution time - "
				+ differenceInNano + " ns");
	}
}

Output:

Original Integer List - [5, 9, 11, 2, 8, 21, 1]

Largest number in List is - 21

Execution time - 0 ns

3. Points to remember w.r.t execution time:

  • Execution time differs in different platforms
  • With small set of numbers, we may not find large difference in execution time
  • But with large set of numbers, difference will be significant to consider

Related Articles:

Happy Coding !!
Happy Learning !!

Java – Find Smallest number in an Arrays or List ?
Java 8 – Find First and Last entries in a Map or HashMap ?