Find the Second Largest Element in an Array

Objective: Given an array of integers. find the second largest element in the array.

Example:

int[] A = { 1, 2, 10, 20, 40, 32, 44, 51, 6 };

Second largest Element : 44

Approach:

  • Keep track of largest element and when ever you change the value of largest element, store its current value to another variable, call it as second largest element.
  • If you are not updating the largest element then check if second largest element is less than the current element, if yes then update it.

Complete Code:

Second largest Element : 44

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

  • Ajitav Mohanty

    Please find an optimized solution:

    “http://quickinfobox.blogspot.in/2016/03/an-optimised-approach-to-find-second.html”

%d bloggers like this: