Program to Find the second largest element in an array.



Program to Find the second largest element in an array...

Answer / Viresh Kumar

To find the second-largest element in a Java array, you can use two variables: one for the largest element and another for the second-largest element. Here's a simple implementation:n```nint max = Integer.MIN_VALUE;nint secMax = Integer.MIN_VALUE;nfor (int num : arr) {n if (num > max) {n secMax = max;n max = num;n } else if (num > secMax && num != max) {n secMax = num;n }n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

How do you override a private method in java?

1 Answers  


Which data type is a class in java?

1 Answers  


What is escape analysis algorithm in JVM and how garbage collection actually worked n how it transfer the objects from one kind of space to other?

0 Answers  


can you create interface instance ?

54 Answers   Fidelity, TCS,


How does thread synchronization occurs inside a monitor? What levels of synchronization can you apply?

1 Answers  


Define array. Tell me about 2-D array.

1 Answers   Agilent,


Can string be considered as a keyword?

1 Answers  


String is a immutable objects . it means that string does not change........... But it will be chang......... { String s="kapil"; String s1="raj"; String s=s1; then print(.......) The String has been changed .. how it is possible and why its called immutable objects

7 Answers  


How to split a string in java?

1 Answers  


What is meant by nested loop?

1 Answers  


What is keyword auto for?

1 Answers  


When does the compiler supply a default constructor for a class?

9 Answers   TCS,


Categories