-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion 9.py
More file actions
26 lines (19 loc) · 1.11 KB
/
Question 9.py
File metadata and controls
26 lines (19 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Concept name : Array
# Question : Second Largest
# Given an array of positive integers arr[], return the second largest element from the array. If the second largest element doesn't exist then return -1.
# Note: The second largest element should not be equal to the largest element.
# Examples:
# Input: arr[] = [12, 35, 1, 10, 34, 1]
# Output: 34
# Explanation: The largest element of the array is 35 and the second largest element is 34.
# Solution :
class Solution:
def getSecondLargest(self, arr):
unique=list(set(arr))
if len(unique)<2:
return -1
unique.sort()
return unique[-2]
# Explanation : First convert the list to a set to remove duplicates, since we're only interested in distinct elements.
# Then, check if the set has at least two unique elements — if not, return -1 as there is no second largest.
# If it does, sort the list and return the second last element (-2 index), which is the second largest number.