DEV Community

Ajay k
Ajay k

Posted on

Find the Largest Number in the Array

Today let's see how we can find the largest number in the unsorted array

lets see different solution

  1. using two for loops
  2. Using max variable
  3. Using built-in sort

Lets see the two for loop method

int findLargestNumber(int[] arr) {
    for (int i = 0; i < arr.length; i++) {
      boolean flag = true;
      for (int j = 0; j < arr.length; j++) {
        if (arr[j] > arr[i]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        return arr[i];
      }
    }
    return -1;
  }
Enter fullscreen mode Exit fullscreen mode

In this we are taking the array as arr
we are going to create a for loop from zero to the length of the array (arr.length)

first in the i loop we are considering the element in the index i as largest element in the array , so we are setting flag to true

now in the j loop we are going to check is there any element greater than the element present in the index i

if its true its going to set the flag false break the loop

since the flag is false it wont enter the if block and the i will be moved to next index this will happen until the i moves to the largest element in the array when i points to the largest element the j loop wont set the flag to false so it will enter if block and will return the largest element

Time complexity O(n2)

Lets now see how we can make this O(n)

int findLargestNumber2(int arr[]) {
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < arr.length; i++) {
      if (max < arr[i]) {
        max = arr[i];
      }
    }
    return max;
  }
Enter fullscreen mode Exit fullscreen mode

In this method we are going to declare a variable max and we initialise it with Integer.MIN_VALUE(-2147483648);

now we just iterate the array with for loop and will check is the current number greater than our max, if true we are going to assign that value to max, when the loop gets over we are returning the max value

Time Complexity:- O(n)

new let's see using built-in sort method

int findLargestNumber3(int[] arr){
    Arrays.sort(arr);
    return arr[arr.length-1];
  }
Enter fullscreen mode Exit fullscreen mode

in this we are using the java built int Arrays.sort() method which will sort us in increasing order so if we need to get the largest element it will present at the last so we are accessing that using arr[arr.length-1],you can also use this method to finding second largest element, first smallest element, second smallest element , n largest or smallest element just by changing the index values

Time Complexity:- O(n log n)

Note:- Arrays.sort() uses As of Java 8, Arrays.sort uses two sorting algorithms. One is a modification of Quicksort named dual-pivot quicksort, the other an adaptation of MergeSort named Timsort. Both have a time complexity of O(n log n)

This is a part of my DSA preparation series
Day 1 to 9 : Analysis of Algorithm
Day 10 : Find the number of digits
Day 11 : Check the given number is palindrome or not
Day 12 : Array basics
Day 13 : Find the Largest Number in the Array

Top comments (0)