forked from me701/python_example_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecond_largest.py
More file actions
34 lines (26 loc) · 848 Bytes
/
second_largest.py
File metadata and controls
34 lines (26 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Python program to find second largest value in a list, e.g., given
[3, 2, 5, 2, 4]
find 4 and, possibly, its location (4).
Adapted from:
www.geeksforgeeks.org/python-program-to-find-second-largest-number-in-a-list/
"""
def second_largest(list1):
mx=max(list1[0],list1[1])
secondmax=min(list1[0],list1[1])
n =len(list1)
for i in range(2,n):
if list1[i]>mx:
secondmax=mx
mx=list1[i]
elif list1[i]>secondmax and mx != list1[i]:
secondmax=list1[i]
else:
if secondmax == mx:
secondmax = list1[i]
return secondmax
if __name__ == "__main__":
# list of numbers - length of list should be at least 2
list1 = [10, 20, 4, 45, 99]
secondmax = second_largest(list1)
print("Second highest number is : ",str(secondmax))