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:
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))