<commit_before># Write a program to find the second largest element in an unsorted array
# Example, for input [1, -4, 3, 7, 9, 0], the output should be7<commit_msg>Added Solution1 to find_2nd_largest_element.rb - 0(nlongn) running time and 0(1) space complexities<commit_after># Write a program to find the second largest element in an unsorted array
# Example, for input [1, -4, 3, 7, 9, 0], the output should be7

# SOLUTION 1: Using the built in sort method - 0(nlogn) running time and 0(1) space
def find_2nd_largest_element(array)
  sorted = array.sort # sorts array in ascending order
  sorted[-2] # returns the 2nd to last element
end


# TEST CASE:
p find_2nd_largest_element([1, -4, 3, 7, 9, 0]) == 7
p find_2nd_largest_element([]) == nil
p find_2nd_largest_element([1]) == nil
p find_2nd_largest_element([1, 1]) == 1

{'fraction_non_alphanumeric': 0.10278113663845223, 'fraction_numerical': 0.049576783555018135, 'mean_word_length': 4.79020979020979, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6689640', 'n_tokens_mistral': 310, 'n_tokens_neox': 275, 'n_words': 114}