Skip to content

Commit eb865a5

Browse files
authored
Merge pull request #13 from priyadarshan1995/master
Program to find second largest element in a list
2 parents 6fbbf90 + 5d458e0 commit eb865a5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

second-largest-num.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Program to find second largest element in list
2+
# Time Complexity : O(N)
3+
# run cmd: python3 second-largest-num.py
4+
5+
class Solution:
6+
def secondLargest(self, l):
7+
mx=max(l[0],l[1])
8+
secondmx=min(l[0],l[1])
9+
n =len(l)
10+
for i in range(2,n):
11+
if l[i]>mx:
12+
secondmx=mx
13+
mx=l[i]
14+
elif l[i]>secondmx and mx != l[i]:
15+
secondmx=l[i]
16+
return secondmx
17+
18+
print("Second Largest number is :", Solution().secondLargest([12,412,11,42,10,1]))

0 commit comments

Comments
 (0)