• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 552
  • Last Modified:

how to find second largest element in an array?

how to find second largest element in an array?

for eg:
input:34,56,23,31,90
ouput:56
0
ganeshkumar_cse
Asked:
ganeshkumar_cse
  • 3
  • 3
  • 2
  • +2
3 Solutions
 
CEHJCommented:
Sort it:

int longest = sortedArray[sortedArray.length - 2];
0
 
CEHJCommented:
(My variable should have been called 'secondLongest')
0
 
cmalakarCommented:
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
magicdlfCommented:
           int max1=array[0], max2=array[1];
            for (int i = 2; i < array.Length; i++)
            {
                if (array[i] > max2)
                {
                    max2 = array[i];
                }
                if (max2 > max1)
                {
                    int tmp = max1;
                    max1 = max2;
                    max2 = tmp;
                }
            }
            //Max2 is the answer you need
0
 
ganeshkumar_cseAuthor Commented:
Ref:int longest = sortedArray[sortedArray.length - 2];

is sortedArray is predefined function?

cant get ur code?
0
 
cmalakarCommented:
if you have..

int x[] = new int[]{3, 2, 1, 4}

You can do..

Arrays.sort(x)

//Now your x will be a sorted array.
0
 
magicdlfCommented:
Sorting takes O(nlogn) while this solution only takes O(n). (I fixed the bug in the last one)

int max1=array[0], max2=array[1];
if (max2 > max1)
{
    int tmp = max1;
    max1 = max2;
    max2 = tmp;
}
for (int i = 2; i < array.Length; i++)
{
    if (array[i] > max2)
    {
        max2 = array[i];
    }
    if (max2 > max1)
    {
        int tmp = max1;
        max1 = max2;
        max2 = tmp;
    }
}
0
 
cmalakarCommented:
after the above lines..

According to CEHJ #ID:26073737, it is...

x[x.length - 2] will give the second largest.
0
 
CEHJCommented:
ganeshkumar_cse, can you tell me why you accepted that particular answer?
0
 
phoffricCommented:
If you have an array of say, a million elements, and you truly just want only the 2nd largest element, then there is an much faster way to do this, since, for example, you don't need the largest element, and you don't need the last element. Now that I've said that, I guess the approach is pretty obvious.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now