Find the Second Largest Number in an Array

Write a program to find the second largest number in an array. Given an unsorted array, we have to write a code to find the second largest number in an array. Also, we need to focus on time complexity. As the time complexity of an algorithm is very important in terms of an interview.

For example – Let’s take an array.

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

Output – 8

The second largest element in this array is 8.  Think for a moment, how do you find the second largest number in an array?  There are multiple ways to solve this problem. Which approach you prefer and why?

In this tutorial, we cover following things –

  • How to find the second largest number in an array using sorting.
  • C program to find the second largest number in an array.
  • C++ program to find the second largest number in an array.
  • PHP code to find the second largest number in an array.
  • Java code to find the second largest number in an array.
Find the Second Largest Number in an array

Find the Second Largest Number in an array

Find the Second Largest Number in an Array using Sorting

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 number.

For sorting an array, we can use following sorting algorithms.

i) Selection Sort

Subscribe Our Tutorials

Get Latest Updates on Facebook

ii) Insertion Sort

iii) Bubble Sort

iv) Merge Sort

v) Quick Sort

Selection sort, insertion sort, and bubble sort are not suitable for large datasets as their time complexity is O(n2) . We can use Merge Sort or Quick Sort for large data sets. The time complexity of merge sort and quick sort is O(nlogn).

NOTE –  If  the element 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.

Find Second Highest Number in an Array using Single Loop

The best approach is to traverse an array to find the second largest number in an array.  The time complexity of this approach is O(n).

C Program to Find the Second Largest Number in an Array

 

C++ Program to Find the Second Largest Number in an Array

We have written a c code to find the second largest number in an unsorted array. Let’s write a c++ code to find the second highest number in an array.

 

PHP Code to Find the Second Largest Number in an Array

PHP provides several functions for sorting an array. After sorting an array, we can easily print the second largest element.

But let’s do without using any inbuilt functions.

How to Sort String in PHP.

Conclusion

There are multiple ways by which you can solve this problem efficiently. I have explained two approaches. If you want to share any other approach to solve this problem efficiently then let us know through your comment.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , . Bookmark the permalink.