Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Find the Second Largest Element in an Array

Objec­tive: Given an array of inte­gers. find the sec­ond largest ele­ment in the array.

Exam­ple:

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

Second largest Element : 44

Approach:

  • Keep track of largest ele­ment and when ever you change the value of largest ele­ment, store its cur­rent value to another vari­able, call it as sec­ond largest element.
  • If you are not updat­ing the largest ele­ment then check if sec­ond largest ele­ment is less than the cur­rent ele­ment, if yes then update it.

Com­plete Code:

Second largest Element : 44

You may also like...

  • Aji­tav Mohanty

    Please find an opti­mized solution:

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