Created
May 17, 2019 14:40
-
-
Save byhbt/b5dea33cb7db60626eed8395ac94e2c4 to your computer and use it in GitHub Desktop.
Find second largest number in an array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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