?
Solved

how to find second largest element in an array?

Posted on 2009-12-17
10
Medium Priority
?
550 Views
Last Modified: 2012-05-08
how to find second largest element in an array?

for eg:
input:34,56,23,31,90
ouput:56
0
Comment
Question by:ganeshkumar_cse
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 600 total points
ID: 26073737
Sort it:

int longest = sortedArray[sortedArray.length - 2];
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26073740
(My variable should have been called 'secondLongest')
0
 
LVL 23

Expert Comment

by:cmalakar
ID: 26073757
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Assisted Solution

by:magicdlf
magicdlf earned 600 total points
ID: 26073813
           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
 

Author Comment

by:ganeshkumar_cse
ID: 26073855
Ref:int longest = sortedArray[sortedArray.length - 2];

is sortedArray is predefined function?

cant get ur code?
0
 
LVL 23

Accepted Solution

by:
cmalakar earned 800 total points
ID: 26073861
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
 
LVL 9

Expert Comment

by:magicdlf
ID: 26073890
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
 
LVL 23

Expert Comment

by:cmalakar
ID: 26073892
after the above lines..

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

x[x.length - 2] will give the second largest.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26073909
ganeshkumar_cse, can you tell me why you accepted that particular answer?
0
 
LVL 33

Expert Comment

by:phoffric
ID: 26074669
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 14 hours left to enroll

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question