Arrays and Hashing Array basics

47. Largest Element

Easy

You are given an array of integers nums. Return the largest value in it.

The answer is a value, not a position. If the largest value appears more than once, return that value.

Example 1

Input: nums = [12, 5, 31, 7, 31, -8]

Output: 31

Explanation: 31 is the largest value. It appears twice, and the answer is the value, not a position.

Example 2

Input: nums = [-7, -3, -12]

Output: -3

Explanation: Every element is negative. The largest is the one closest to zero, −3.

Example 3

Input: nums = [42]

Output: 42

Check your understanding

Work out the answer for this input before you write any code.

nums = [-5, -1, -9, -1]

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.