Array Second Largest Element in Python
In this class, We understand the logic for Array Second Largest Element in Python.
For Complete YouTube Video: Click Here
Array Second Largest Element
The reader should have prior knowledge of python. Click here.
Let’s take an example and understand the question.
Second Largest Element if duplicates are considered
Example: [1,8,4,6,7,8] Take a list of elements.
In the above list, element 8 is the largest number, and we have element 8 two times.
The second biggest element is also eight if duplicates are considered.
Writing the logic for this example using inbuilt python methods is very easy.
Logic: Sort the elements in the list using the sort method and take the second element from last.
Sometimes writing logic using inbuilt methods is not efficient.
Let’s understand why not efficient?
Sorting the elements will take nlogn time if n is the number of elements.
In this example, we can write logic that executes in n time.
Suppose you are not good at calculating the time complexity of the algorithms. Would you please check our design and analysis of algorithms course?
The code to the logic mentioned above is given below.
# find the second largest element if duplicates are allowed to take as second largest
n=int(input("enter the number of elements >2----"))
print("enter elements")
list1=[]
for i in range(n):
list1.append(int(input()))
list1.sort()
print("the second largest element is ",list1[-2])
Second Largest Element if duplicates are not considered
Logic: Eliminate duplicates using set data type in python.
After eliminating duplicates. Sort the elements and take the second element from last.
The code for the logic mentioned above is given below.
# find the second largest element if duplicates are not allowed to take as second largest
n=int(input("enter the number of elements >2----"))
print("enter elements")
list1=[]
for i in range(n):
list1.append(int(input()))
removedup=set(list1)
afterremovedup=list(removedup)
afterremovedup.sort()
if len(afterremovedup)>1:
print("the second largest element ",afterremovedup[-2])
else:
print("there is no second largest element")
Output:
enter the number of elements >2----7
enter elements
8
8
9
0
9
1
3
the second largest element 8
Writing code without using inbuilt functions is essential.
We will provide the logic to identify the second largest element if duplicates are allowed to take.
Logic: Take two variables, largest and second-largest.
These variables are updated with the first two values in the list.
The largest element in the first two elements is assigned to variable largest.
The second-largest variable is set with the remaining number.
From the 3rd element, update the largest and second-largest variables.
If the new element is greater than the largest element. The second-largest will be the largest element, and the largest will be the new element.
The second condition. Suppose the new element is greater than the second-largest and less than or equal to the largest.
Update second largest with the new element.
n=int(input("enter the number of elements >2----"))
print("enter elements")
list1=[]
for i in range(n):
list1.append(int(input()))
if list1[0]>list1[1]:
largest=list1[0]
slargest=list1[1]
else:
largest=list1[1]
slargest=list1[0]
for j in range(2,n):
if(list1[j]>largest):
slargest=largest
largest=list1[j]
elif list1[j]>slargest and list1[j]<=largest:
slargest=list1[j]
print(slargest)
Output:
enter the number of elements >2----7
enter elements
8
6
11
9
1
5
12
11
The step by step explanation of the code is given in the video.