Find second largest in an array of positive integers

Given an array of positive integers, return the second largest element from the array. If the second largest element doesn't exist then return -1.

Hint

Arrange the numbers in the list from smallest to largest (ascending order). Identify the largest number in the list. Start from the second-to-last number in the list and move towards the beginning. At each position, compare the current number with the largest number. If the current number is smaller than the largest number, return that number.

# Python implementation
ls = [20, 5, 20]

ls.sort()

largest = ls[-1]
output = -1

for i in range(len(ls) - 2, -1, -1):
  if ls[i] < largest:
    output = ls[i]
    break

print(output)
// Javascript implementation
const list = [20, 5, 20];

list.sort((a, b) => a - b);

const largest = list[list.length - 1];
let output = -1;

for (let i = list.length - 2; i >= 0; i--) {
  if (list[i] < largest) {
    output = list[i];
    break;
  }
}

console.log(output);