CodeTestValidate

No installations needed! Write Python directly in your browser and get instant feedback with our smart validation system.

Challenge # 4

Find the Second-Largest Number

Objective

Identify the second-largest number from a list of integers.

Instructions

  • You are given a list of integers named nums.

  • Write a function second_largest(nums) that returns the second-largest number in the list.

  • Assume the list contains at least two distinct numbers.

  • Do not use built-in sorting methods like sort() or sorted().

  • Your solution should work efficiently even for large lists.

Constraints:
Do not use input(). • Assume the list contains at least two distinct elements. • Do not use built-in sort functions like `sort()` or `sorted()`.
Exemple IO

Input: nums = [4, 1, 7, 3, 9, 2]

Output: 7

Input: nums = [10, 20, 5, 20, 8]

Output: 10

Input: nums = [-1, -5, -3, -4]

Output: -3

Try It Online!

Output:

Validate Your Results

Get your code reviewed and validated to understand what you did right and where you can improve.

Previous challengeBinary SearchNext challengeCount Words in a Sentence (Ignore Punctuation)