March 1, 2017

# How to find second largest number in python

**Second largest number python problem**

Given an array of numbers, write python code to find second largest number in array

**Code to find second largest number in list**

# Array A = [1, 5, 7, 5] # Initialize first and second maximum max1 = A[0] max2 = A[1] if A[1] < A[0]: max1 = A[1] max2 = A[0] # Find the first and second maximum for num in A: # if the current element is greater # than the first maximum then we # should update it if num >= max1 and num < max2: max1 = num # Otherwise compare the current element # with the second maximum and update it # similarly elif num >= max2: max1 = max2 max2 = num # Otherwise keep the first and # second maximum intact else: # Nothing pass # Print print "Second largest number is %d" % max1

A C++ version can also be found here.

Thanks for visiting. Please use the comments section for feedback.

Search Terms...