Skip to content

Instantly share code, notes, and snippets.

@byhbt
Created May 17, 2019 14:40
Show Gist options
  • Save byhbt/b5dea33cb7db60626eed8395ac94e2c4 to your computer and use it in GitHub Desktop.
Save byhbt/b5dea33cb7db60626eed8395ac94e2c4 to your computer and use it in GitHub Desktop.
Find second largest number in an array
def second_largest(given_list):
len_of_list = len(given_list)
if len_of_list == 1 or len_of_list == 0:
return None
if len_of_list == 2:
if given_list[0] > given_list[1]:
return given_list[1]
else:
return given_list[0]
largest = given_list[0]
second_largest = given_list[0]
for item in given_list:
if (item > largest):
second_largest = largest
largest = item
return second_largest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment