Write a Program to find the second largest number in an unsorted array. Given an array of unsorted values find the second largest number in an array.

For example – Let’s take an array.

arr[] = { 4, 1, 5, 2, 8, 0, 3, 55}

The second largest element in this array is 8. So how do you find the second largest number in an array ? There are multiple ways by which you can solve this problem.

Find Second Largest Number in an Array

**First Approach : Sort an Array**

The simple approach is to sort an array using sorting algorithm. After sorting an array **element at n-2 index** will be the second largest element.

You can use following sorting algorithms.

i) Selection Sort

ii) Insertion Sort

iii) Bubble Sort

iv) Merge Sort

v)Quick Sort

Selection sort, insertion sort and bubble sort are not suitable for large data sets as their **time complexity** is **O(n**^{2}) . You can use Merge Sort or Quick Sort for large data sets.

**NOTE** – If values of an array is repeated then this approach won’t work. Then the best approach is to traverse an array to find the second largest element.

**Second Approach : Traverse an array**

The best approach is to traverse an array to find the second largest number in an array. Using this approach maintain two flags one for highest and other for the second highest number. The time complexity of this approach is O(n).

→

1Comment