Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Finding the second largest
element from a given list
method 1 Sort the list and
remove the largest element and
print max
method 2 sort the list and
using negative indexing print
the second max
method 3 without sorting find
the max of list then find the
next max of the list
by comparing the max
Design algorithm for the
above problem

What I have tried:

Python
def find Largest(list):
sec_Larg = 0
largest = min(list)
for i in range(len(list)):
if list[i] > largest:
sec_Larg = 
largest
largest = list[i]
else:
sec_Larg = 
max (sec_Larg, list[i])
return sec_Larg
def sec_larg_elem_m1(list):
 list.sort()
 max_elem = max (list [:-1])
 return max_elem
def sec_larg_elem_m2(list):
 list.sort()
 sec_larg = list [-2]
 return sec_larg
def sec_larg_elem_m3(list):
 max_element = max(list)
 list.remove(max_element)
 sec_larg = max(list)
 return sec_larg
list = [1,5,2,8,9,3,7]
print ("Second largest number:
",findLargest(list))
print ("Second largest number 
using Method-1: 
",sec_larg_elem_m1(list))
print ("Second largest number 
using Method-2: 
",sec_larg_elem_m2(list))
print ("Second largest number 
using Method-3:
",sec_larg_elem_m3(list))
Posted
Updated 22-Mar-23 6:53am
v2
Comments
Richard MacCutchan 22-Mar-23 10:59am    
Do you have an actual question?

1 solution

Start here: Python Indentation[^]

Unless you indent Python code correctly - and I mean exactly correctly - it will not work as you intended.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900