Given a list of numbers, the task is to write a Python program to find the second largest number in given list.
Input: list1 = [10, 20, 4] Output: 10 Input: list2 = [70, 11, 20, 4, 100] Output: 70
Method 1: Sorting is an easier but less optimal method. Given below is an O(n) algorithm to do the same.
Second highest number is : 45
Method 2: Sort the list in ascending order and print the second last element in the list.
Second largest element is: 45
Method 3: By removing the max element from the list
Method 4: Find max list element on inputs provided by the user
Enter number of elements in list: 4 Enter elements: 12 Enter elements: 19 Enter elements: 1 Enter elements: 99 Second Largest element is: 19
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.