Click4Ai

6.

Easy

Write a function second_largest(arr) that finds the second largest element in an array. You may assume the array has at least two distinct elements.

Example:

Input: arr = [5, 2, 8, 1, 9, 3]

Output: 8

Constraints:

  • Array length: 2 <= len(arr) <= 1000
  • Array contains at least two distinct values
  • Do not use sorting or built-in max()
  • Test Cases

    Test Case 1
    Input: [5, 2, 8, 1, 9, 3]
    Expected: 8
    Test Case 2
    Input: [10, 20, 30]
    Expected: 20
    Test Case 3
    Input: [1, 1, 2, 2, 3]
    Expected: 2
    + 2 hidden test cases