In this lecture, we have to find the second largest element in an array.
In the previous lecture, we have seen how we can calculate the largest and smallest element in an array.
This program is a bit different because now we have to find the second-largest element in an array.
How are we going to do so?
It is simple. First of all, we have to find the largest element as well in the array because then only we can decide which is the second largest.
To find the desired results and the second largest element in an array, we will take two variables max 1 & max 2.
We will take the first two elements of the array and store the maximum of both to max 1 and the other to max 2.
Now we will run a loop from the third element to the end of the loop with if & else condition such that:-
if a[i] is greater than max 1 store that in max 1 else if a[i] is greater than max 2 store that in max 2.
In this way, once the loop will end, we will be able to get our desired results.
A similar logic can be used to find the second smallest element of an array with slightly different conditions.
Let's see the java program to find the second largest element in an array.
Subscribe to our channel for regular updates on the dsa course and click on the bell icon to never miss an update from our dsa course.
Data Structures and Algorithms Free Course (Learn DSA Without Paise) Playlist - • Data Structures And Algorithms Full Course...
For more information, fill this form: https://forms.gle/8eiUmM92Fx563Aen9
or call us at 8884881203
Facebook: / thetapacademy
Instagram: / tapacademy_online
Linkedin: / 73820805
Website: https://www.thetapacademy.com
#dsa #dsacourse #java #dsainjava #javaprogramming #coding #coder #placement #array #arrayquestion #arrayjava
On this page of the site you can watch the video online Second Largest Element In An Array In Java | FREE DSA Course in JAVA | Lecture 50 with a duration of hours minute second in good quality, which was uploaded by the user TAP ACADEMY 17 January 2023, share the link with friends and acquaintances, this video has already been watched 65,363 times on youtube and it was liked by 1.4 thousand viewers. Enjoy your viewing!