2nd max number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

2nd max number

How to find second maximum number in array using only one for loop in java?

15th Dec 2021, 8:43 PM
Muhammad Adeel
3 Answers
+ 3
Are there possibly duplicate values in the array? Here some ideas how it could be approached: * Sort the array (ascending) and then take the 2nd last * find the max, create a new array without max and get the max of the new array * loop through the array and check if the current value is larger than any previous one and smaller than the max
15th Dec 2021, 8:57 PM
Lisa
Lisa - avatar
+ 2
Martin Taylor yes, I wondered whether there could be duplicate values... I guess it depends on the teacher's intention with the task :)
15th Dec 2021, 9:11 PM
Lisa
Lisa - avatar
+ 1
Martin Taylor, Lisa Maybe something like this? Only one loop and no sort: https://code.sololearn.com/cvLEzLgWYrC2/?ref=app
15th Dec 2021, 11:03 PM
Coding Cat
Coding Cat - avatar