Find second largest number in list ?------need detailed explanation in this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Find second largest number in list ?------need detailed explanation in this code

n=int(input()) list1=list(map(int,input().split(" ")[:n])) list1.sort() c=list1.count(max(list1)) r=list1[len(list1)-(c+1)] print(r)

9th Sep 2022, 6:54 PM
NAVEEN VYAS
4 Answers
+ 8
NAVEEN VYAS , n=int(input()) # define the number of values to input list1=list(map(int,input().split(" ")[:n])) # input the desired number of values separated by a space. this creates a list of the input numbers as integer values list1.sort() # sort the list inplace in ascending order c=list1.count(max(list1)) # count how often the max value exists in the list r=list1[len(list1)-(c+1)] # pick the second largest number by using a calculated index print(r) # print the second largest number
9th Sep 2022, 8:56 PM
Lothar
Lothar - avatar
+ 1
The ...[:n] in line 2 is used to specify the number of elements to return in list created using split.
11th Sep 2022, 6:08 AM
boniface
0
Then y [:n] in second line
10th Sep 2022, 7:49 PM
NAVEEN VYAS
0
What it indicates
10th Sep 2022, 7:50 PM
NAVEEN VYAS