Finding second largest number in the array

Finding the largest element or number in the array is easily done by comparing the first element with all elements in the list and update the variable max as the current element.

If you were given a list findmax=[1,3,5,10,7] 

then maximum value= 10

second maximum = 7

Logic:

To find the second maximum in the list. Take two variables max and secondmax and initialize them with the first two elements in the list. Then, run a loop from 1 to n. If the compared element is larger than max then

secondmax=max;

max=findmax[i];


Now, we are going to do this in python. When you program in python you didn't need yourself to stick to one process. There are many inbuilt methods that make your job easy. we can discuss that later.

Program:

l=list(map(int,input().split()))

s=list(set(l))

if(len(s)==1):

    print("There is no second max in the list")

else:

    maxi=s[0]

    secondmaxi=s[1]

    for i in range(1,len(s)):

        if(maxi<s[i]):

            secondmaxi=maxi

            maxi=s[i]

        elif(secondmaxi<s[i]):

            secondmaxi=s[i]

print("second maximum element in the list is ", secondmaxi)


I didn't know about other programs on the internet, but this passes all the test cases you provided.


The logic is simple as I explained above comparing max with every element so if any element is larger than max then make maximum as the second maximum and the present element becomes a maximum element.

you may have doubt why I am included set in the program. 

Note: I used maximum as maxi variable and second maximum as secondmaxi.

Testcase 1) l=[1,3,5,10,7]

maxi=1

secondmaxi=3


1st iteration (3<3) - Wrong Condition; secondmaxi=3;

2nd iteration (3<5) - maxi=5; secondmaxi=3

3rd iteration (5<10) - maxi=10; secondmaxi=5

4th iteration (10<7) - Wrong Condition; secondmaxi=7

Therefore second maximum in the list is 7.


Test case 2) There are some cases like l=[7,4,7]

When you implement the same code without python set the program returns 9 as both maxi and secondmaxi.

If you apply set then l=[7,4]

Then running the above program returns secondmaxi as 4.


Test Case 3) This is a tricky case. When you have given a list l=[7,7,7,7]

What would be your output?

There is only one identical element present multiple times. There is no second maximum present in it. So if (len(s)==1):

It returns no second maximum.


Try yourself with different test cases and comment on the tricky cases?


Another method to find this is by Sorting. The logic is simple as you can sort the list in descending order and return the second element or vice-versa.

l=list(map(int,input().split()))

s=sorted(l)

print("second maximum element in the list is ", s[2])


I will leave you to apply the set in the above program.


Another method is to delete the largest element in the set and now the maximum in the list becomes the second maximum.

l=list(map(int,input().split()))

l.remove(max(l))

print("second maximum element in the list is ", max(l))


remove method in python removes the element gives as the parameter.


You can use any of the above methods. All the methods in the line of beginner level to moderate level. I am also a student and interested to know any better methods are there. So please comment on them.


If you interested in Python Projects read

Website Blocker: https://alwayscodin.blogspot.com/2020/06/website-blocker-using-python.html

Web Scraping: https://alwayscodin.blogspot.com/2020/08/web-scraping-simple-tutorial-scraping.html


Subscribe to weekly updates of coding.

Post a Comment

Previous Post Next Post