find the second largest number in a list

Solutions on MaxInterview for find the second largest number in a list by the best coders in the world

showing results for - "find the second largest number in a list"
Jamie
16 Jun 2019
1def second_largest(numbers):
2  if (len(numbers)<2):
3    return
4  if ((len(numbers)==2)  and (numbers[0] == numbers[1]) ):
5    return
6  dup_items = set()
7  uniq_items = []
8  for x in numbers:
9    if x not in dup_items:
10      uniq_items.append(x)
11      dup_items.add(x)
12  uniq_items.sort()    
13  return  uniq_items[-2]   
14print(second_largest([1,2,3,4,4]))
15print(second_largest([1, 1, 1, 0, 0, 0, 2, -2, -2]))
16print(second_largest([2,2]))
17print(second_largest([1]))
18
19
queries leading to this page
how to find second largest number in list in python without any keywordsecond largest in list pythonfind second highest in list pythonpython list second largestpython find the second largest number in a listpython get second largest number in listsecond highest value in list pythonprint second highest number in list pythongiven a list of numbers find the second largest number in the list in pythonhow to find the second largest number in a list pythonsecond largest value in list pythonsecond largest number in python listsecond largest element in a listpython list which return second largest number in the listsecond largest element in list pythonsecond largest and large largest from the list in pythonsecond largest in list pythoblargest and second largest number pythonfind second largest number in list pythonfind second largest number in array position pythonpython second largest number in listsecond largest number in the listhow to find the second largest number if it exist more than once in the list in pythonsecond largest number in list pythonpython program to find second largest number in a listhow to get second largest number in list pythonfind the second highest number in listfind the second largest number in list pythonsecond largest value of array pythonsecond largest number in array pythonpython find second largest value in listhow to find second highest number in list pythonpython find second highest value in listfind second largest number in list python using maxsecond largest element in list in pythonprint second largest number in list pythonget second largest value in list pythonfind second largest number in python listhow to find the second highest number in a list pythonhow to find second maximum number in list pythonfind the second largest number in an array pythonsecond largest number in python from listfind the second largest number in a list