
Python HackerRank! Find the Runner-Up Score - AKA Finding the second largest number in an array, list or tuple.
Apr 18
1 min read
0
4
0

Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.
Required Output: 5
Given list: 2 3 6 6 5
At first glance, this looks like a simple case of sorting the list and returning the value at index 1. However, HackerRank have been sneaky and the max occurs twice, so the code below is required.
if name == '__main__':
n = int(input())
arr = map(int, input().split())
x = list(arr)
y = max(x)
#print(y)
new_list_without_max = [item for item in x if item != y]
print(max(new_list_without_max))
First, turn the array into a list.
Secondly, return the max value.
Thirdly, write the values to a new list if their value is not the maximum.
Then, return the maximum value of the second list.
Finally, Robert is your father's brother.