Python Forum
Find the second largest number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find the second largest number
#1
The task is to find the second largest number in an array.
For eg
INPUT: 5
1, 4, 5, 5, 3
OUTPUT: 4

I tried the following code, but it shows up an error that map() has no len().
def runner_up(arr):
    a = max(arr)
    for i in range(len(arr)):
        if arr[i] == a:        
            arr.remove(a)  #remove all the maximum values. There can be more than one
    return max(arr)

size_of_arr = int(input())
arr = map(int, input().split())
print (runner_up(arr))
If I don't use map, I will not be able to take the input and if I use map, I will not be able to get the length of the array. What should I try? I will like to know how to fix my code and also if there are any other methods to solve this problem.
Reply
#2
With some tiny changes:

#!/usr/bin/python3
def runner_up(arr):
    a = max(arr)
    for i in range(len(arr)):
        if arr[i] == a: 
            arr.remove(a)
            break
    return max(arr)
 
arr = list(map(int, input().split()))
print (runner_up(arr))
Reply
#3
Another approach is 'find maximum value from elements of list which are not equal to maximum element value in list' :-)

>>> lst = [1, 4, 5, 5, 3] 
>>>  max([num for num in lst if num != max(lst)]) 
4
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
perfringo solution is pythonic
Reply
#5
The "standard" way is to sort the list and choose whatever you want.
Reply
#6
my_list = [1, 4, 5, 5, 3]
second_highest = sorted(set(my_list))[-2]
print(second_highest)
Output:
4
That's just what woooee described
Reply
#7
You are welcome.
Reply
#8
Just observation: code must be more defensive in real life usage - edge cases must be handled (empty list, all list elements with same value etc).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
Exactly.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find the sum of a series of values that equal a number ancorte 1 493 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,188 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Find if chain of characters or number Frankduc 4 1,796 Feb-11-2022, 01:55 PM
Last Post: Frankduc
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,628 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  find 2 largest equal numbers Frankduc 13 3,532 Jan-11-2022, 07:10 PM
Last Post: Frankduc
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,410 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Largest product in a grid (projecteuler problem11) tragical 1 2,281 Sep-14-2020, 01:03 PM
Last Post: Gribouillis
  Extract the largest value from a group without replacement (beginner) preliator 1 2,074 Aug-12-2020, 01:56 PM
Last Post: DPaul
  frequency of largest number group anshumanmuj 5 2,991 Jun-22-2020, 04:51 PM
Last Post: perfringo
  Sort by the largest number of the same results (frequency) inlovewiththedj 3 2,206 Apr-01-2020, 07:29 AM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020