Find the Runner-Up Score! in Python - Hacker Rank Solution


Find the Runner-Up Score! in Python - Hacker Rank Solution


Problems:


Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given n scores. Store them in a list and find the score of the runner-up.



Input Format :

The first line contains n. The second line contains an array A[] of n integers each separated by a space.

Constraints :

  • 2 <= n <= 10
  • -100 <= A[i] <= 100

Output Format :

Print the runner-up score.



Sample Input :

5
2 3 6 6 5

Sample Output :

5

Explanation :

Given list is [2,3,6,6,5]. The maximum score is 6, second maximum is 5. Hence, we print 5 as the runner-up score.



Solution :


1
2
3
4
5
6
7
# Find the Runner-Up Score! in Python - Hacker Rank Solution
if __name__ == '__main__':
    n = int(raw_input())
    arr = map(int, raw_input().split())
    # Find the Runner-Up Score! in Python - Hacker Rank Solution START
    print(sorted(list(set(arr)))[-2])
    # Find the Runner-Up Score! in Python - Hacker Rank Solution END





Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.

Next Post Previous Post
54 Comments
  • P. Pradeep
    P. Pradeep Friday, December 04, 2020

    if __name__ == '__main__':
    n = int(input())
    arr=[]
    arr.append(input().split())
    c=arr[0]
    d=[]
    for i in range (len(c)):
    d.append(int(c[i]))
    x=sorted(d,reverse=True)
    for i in range (len(x)):
    if (x[0]==x[i+1]):
    continue
    else:
    print(x[i+1])
    break

    • Vidya
      Vidya Tuesday, August 24, 2021

      hi, this code works fine until we give combination of two digit and three digit number in the scores.
      Please help to write sorting command to sort based on length of int and in descending order.

    • Unknown
      Unknown Sunday, November 28, 2021

      arr=sorted(arr)
      l=[]
      for i in range(len(arr)):
      if arr[i]<max(arr):
      l.append(arr[i])
      print(max(l))

    • Nagesh Madhu
      Nagesh Madhu Thursday, April 07, 2022

      i love you

    • Anonymous
      Anonymous Sunday, May 15, 2022

      you are that guy!!

  • Unknown
    Unknown Friday, January 22, 2021

    n = int(input())
    arr = (int, input().split())


    arrlist = []

    for t in arr:
    arrlist.append(t)
    num=arrlist[1]

    test_list = [int(i) for i in num]
    test_sorted= (sorted(test_list, reverse=True))
    List = []
    for i in test_sorted:
    if i != max(test_sorted):
    List.append(i)

    print(max(List))

  • Alok Kumar
    Alok Kumar Thursday, May 27, 2021

    def find_highscore(data, n):
    high_score = 0
    data = sorted(data,reverse=True)
    for i in range(len(data)):
    if data[i] > data[i+1]:
    high_score = data[i + 1]
    break
    return high_score

    if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split(" "))
    arr = list(arr)
    print(find_highscore(arr,n))

  • Joy
    Joy Thursday, June 03, 2021

    score_sheet =[]
    st_numbers = int(input("the number of students: "))
    for i in range(st_numbers):
    data = int(input())
    score_sheet.append(data)
    score_sheet.sort(reverse=True)
    score_sheet[1]

    • Soham Jadhav
      Soham Jadhav Wednesday, July 07, 2021

      Easiest solution I found for me as a beginner. Thanks!

    • Unknown
      Unknown Saturday, September 18, 2021

      simple solution , Thanks!

    • Unknown
      Unknown Tuesday, November 30, 2021

      bhi code run nhi kar raha ha

  • Mohammed Shafakhatullah Khan
    Mohammed Shafakhatullah Khan Monday, June 07, 2021

    if __name__ == '__main__':
    n = int(raw_input())
    arr = map(int, raw_input().split())

    arr = list(dict.fromkeys(arr))
    arr.sort()
    arr.reverse()
    print(arr[1])

    • apurv
      apurv Wednesday, November 03, 2021

      it won't return correct answer if input has same number multile times

    • Anonymous
      Anonymous Monday, August 22, 2022

      # use this command for repeated values
      n=int(input())
      arr=[]
      for i in range(n):
      j=int(input())
      arr.append(j)
      while(True):
      c=max(arr)
      arr.remove(c)
      d=max(arr)
      if d==c:
      continue
      else:
      break
      print(d)

  • Unknown
    Unknown Wednesday, August 18, 2021

    n = int(input())
    arr = input().split()
    s = sorted(arr)
    i = n
    while i != 0:
    if s[i-1] != s[n-1]:
    print(s[i-1])
    break
    i -= 1

  • Aishu Writez
    Aishu Writez Monday, August 23, 2021

    if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())
    # Find the Runner-Up Score! in Python - Hacker Rank Solution START
    print(sorted(list(set(arr)))[-2])
    # Find the Runner-Up Score! in Python - Hacker Rank Solution END

  • Lavnya Sharma
    Lavnya Sharma Saturday, August 28, 2021

    if __name__ == '__main__':
    n = int(input())
    #arr = map(int, input().split())
    a=input().split()
    arr=[int(x) for x in a]
    #print(arr)
    maxs=0
    smax=0
    k=len(arr)
    for i in range(0,k):
    if arr[i]>maxs:
    maxs=arr[i]
    #print(maxs)
    for j in range(0,k):
    if arr[j]>smax:
    if arr[j]<maxs:
    smax=arr[j]
    print(smax)

    • Unknown
      Unknown Sunday, December 26, 2021

      but why? you slow down compiler and buffer goes slow.., easiest simply 5-6 lines to code....and you find this it's good

  • Ana
    Ana Friday, September 17, 2021

    #easiest to understand
    n = int(input('Size of the array - '))
    score = []
    for x in range(0,n):
    k = int(input('Scores - '))
    score.append(k)

    score.sort()
    scoreset=set(score)
    sc = list(scoreset)
    print('Runner up element is {}'.format(sc[-2]))

  • Prasath
    Prasath Saturday, September 25, 2021

    n = int(input())
    com =[]
    lis =(input().split())
    lis.sort()
    for i in lis:
    if i not in com:
    com.append(i)
    com.sort()


    print(f'{com[-2]}')

  • kalikirijagadesh
    kalikirijagadesh Saturday, October 09, 2021

    # Online Python compiler (interpreter) to run Python online.
    # Write Python 3 code in this online editor and run it.
    a=[2,6,3,6,5]
    c=[]
    for i in range(len(a)):
    for j in range(len(a)-1):
    if(a[j]>a[j+1]):
    temp=a[j]
    a[j]=a[j+1]
    a[j+1]=temp
    print(a)
    for i in range(len(a)):
    for j in a:
    if j in c:
    break
    else:
    c.append(j)
    print(c)
    b=len(c)-2
    print(c[b])

  • kalikirijagadesh
    kalikirijagadesh Saturday, October 09, 2021

    a=[2,3,6,6,5]
    c=[]
    for i in range(len(a)):
    for j in range(len(a)-1):
    if(a[j]>a[j+1]):
    temp=a[j]
    a[j]=a[j+1]
    a[j+1]=temp
    for j in a:
    if j not in c:
    c.append(j)
    b=len(c)-2
    print(c[b])

  • Srijeeta Dev
    Srijeeta Dev Monday, October 11, 2021

    This comment has been removed by the author.

  • Hessa.w
    Hessa.w Saturday, October 16, 2021

    I am new to programming can somebody please explain what is the problem with this code
    f __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())


    y =[]
    for val in arr:
    if val>0:
    y.append(val)


    print (max(y)-1)

  • Unknown
    Unknown Friday, October 22, 2021

    n = int(input())
    arr = list(map(int, input().split()))
    arr.sort()
    for i in reversed(range(len(arr))):
    if arr[i-1]<arr[i]:
    print(arr[i-1])
    break

  • PDD
    PDD Friday, October 22, 2021

    another one

    if __name__ == '__main__':
    n = int(raw_input())
    arr = map(int, raw_input().split())
    arr.sort(reverse=True)
    for i in range(n):
    if arr[i]>arr[i+1]:
    print(arr[i+1])
    break

    • Unknown
      Unknown Thursday, December 16, 2021

      cheek our coad

    • Anonymous
      Anonymous Saturday, June 25, 2022

      bruh

  • Anonymous
    Anonymous Sunday, November 14, 2021

    n = int(input())
    arr = map(int, input().split())
    new_list = sorted(list(arr))

    first = max(new_list)
    new_list2 = []

    for index in range(len(new_list)):
    if new_list[index] != first:
    new_list2.append(new_list[index])

    print(max(new_list2))

  • Shreya Banerjee
    Shreya Banerjee Wednesday, January 05, 2022

    This comment has been removed by the author.

  • Mayank Kaushik
    Mayank Kaushik Thursday, January 13, 2022

    n = int(input())
    b = map(int, input().split())
    b=list(b)
    d=[]
    e=[]
    (b.sort(reverse=True))
    for i in range(0,n):
    if b[i]<b[0]:
    d.append(b[i])
    break
    else :
    pass
    print(d[0])

  • myfirstblogonthis
    myfirstblogonthis Sunday, January 16, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = list(set(map(int, input().split())))
    print(sorted(arr)[-2])

  • Parag Gupta
    Parag Gupta Wednesday, January 19, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = set(map(int, input().split()))
    arr.remove(max(arr))
    print(max(arr))

  • Jagan Chowhaan
    Jagan Chowhaan Sunday, February 20, 2022

    If we use the sort method to sort the values and there by using slicing operator, it works but only for the given output for the present question on hackerrank.

  • bhanu
    bhanu Thursday, February 24, 2022

    Thanks for providing this information
    Best Python Online Training
    Learn Python Online Course

  • Unknown
    Unknown Monday, February 28, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())
    ar=list(arr)
    ar.sort()
    max=ar[n-1]
    for i in range(0,n):
    if ar[i]==max:
    print(ar[i-1])
    break

  • Anonymous
    Anonymous Friday, March 11, 2022

    if __name__ == '__main__':
    def runnerup(value,k):
    if value == arr[n-1]:
    value =arr[k-1]
    runnerup(value,k-1)
    else:
    print(value)

    n = int(input())
    arr = list(map(int, input().split()))
    arr.sort()
    runnerup(arr[n-2],n-2)

  • MUSKAN SINGH
    MUSKAN SINGH Sunday, March 27, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    v=max(arr)
    arr.remove(v)
    u=max(arr)
    if (u==v):
    arr.remove(u)
    i=(max(arr))
    if (i==u==v):
    arr.remove(i)
    print(max(arr))



    h=max(arr)
    if(u==v==h==i):
    s=sorted(arr)
    print(x[0])




    else:
    print(max(arr))




    else:
    print(max(arr))

  • Hanibaal
    Hanibaal Friday, April 08, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    highest = max(arr)
    while highest in arr:
    arr.remove(highest)
    print(max(arr))

  • Unknown
    Unknown Monday, April 18, 2022

    if __name__ == '__main__':
    n = int(raw_input())
    arr = map(int, raw_input().split())

    a = max(arr)
    arr.sort()
    for i in range(0,n):
    if arr[i] <a:
    b = arr[i]
    print(b)

  • Anonymous
    Anonymous Tuesday, April 19, 2022

    n = int(input())
    arr = list(map(int, input().split()))
    arr.sort(reverse = True)
    m = arr.count(arr[0])
    print(arr[m])

  • Himanshu Sankhala
    Himanshu Sankhala Thursday, April 21, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())

    b = set(arr)
    b.remove(max(b))
    print(max(b))

  • Anonymous
    Anonymous Thursday, April 28, 2022

    x = list(arr)
    #mylist = list(dict.fromkeys(mylist))
    mylist = list(dict.fromkeys(x))
    mylist.sort()
    print(mylist[-2])

  • Anonymous
    Anonymous Saturday, May 14, 2022

    if __name__ == '__main__':
    n = int(raw_input())
    arr = map(int, raw_input().split())

    arr = list(dict.fromkeys(arr))
    arr.sort()
    arr.reverse()
    print(arr[1])

  • Prabhat Karlekar
    Prabhat Karlekar Friday, May 20, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())
    arr=sorted(set(arr))
    print(arr[-2])

  • Anonymous
    Anonymous Thursday, June 02, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split(" "))
    arr = list(arr)

    b=set(arr)
    c=(list(b))
    c.sort()
    print(c[-2])

  • Anonymous
    Anonymous Friday, June 03, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())
    ar=list(arr)
    ar.sort()
    arn=[]
    maxi=max(ar)
    for i in range(n):
    # temp=ar[i]
    if maxi>ar[i]:
    arn.append(ar[i])
    print(max(arn))

  • Anonymous
    Anonymous Monday, June 13, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))

    res = []

    for i in arr:
    if i not in res:
    res.append(i)

    if n==len(arr) and n>=2:
    arr=sorted(res,reverse=True)
    for i in range(n):
    if arr[i]==arr[0]:
    continue
    else:
    print(arr[i])
    break

    elif 1 == (n - len(arr)):
    print("one item is missing")

    elif 1 == n:
    print("Items should be greater than or equal to 2")

    else:
    print("{} items are missing".format((n-len(arr))))

  • Arjun B Raj
    Arjun B Raj Saturday, July 02, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())
    arr_list = list(arr)
    arr_set = set(arr_list)
    arr_list1 = list(arr_set)
    arr_sorted = sorted(arr_list1,reverse=True)
    print(arr_sorted[1])

  • EL MasRy
    EL MasRy Wednesday, July 13, 2022

    This comment has been removed by the author.

  • 오똑해
    오똑해 Thursday, August 04, 2022

    This comment has been removed by the author.

  • saisandeep
    saisandeep Saturday, August 13, 2022

    n = int(input())
    arr = map(int, input().split())
    arr = list(arr)
    l=[]
    x=max(arr)
    for i in range(len(arr)):
    if arr[i]<x:
    l.append(arr[i])
    l.sort()
    runner_up=l[-1]
    print(runner_up)


  • Anonymous
    Anonymous Sunday, August 14, 2022

    if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())

    arr2 = list(arr)
    a = max(arr2)
    for i in arr2:
    if(i==a):
    c = arr2.count(i)
    for x in range(c):
    arr2.remove(i)
    print(max(arr2))

Add Comment
comment url