Find largest number
JavaScript
easy
10 mins
Given an array arr of numbers, return the largest number in the array. If the array is empty, return null.
Examples
Input: arr = [3, 1, 2] Output: 3
Input: arr = [-5, 2, -3, 4] Output: 4
Input: arr = [0, 2, 3] Output: 3
Input: arr = [] Output: null
Constraints
- Input must be an array of finite numbers
- Return false for non-array inputs
- Return false for arrays containing non-number values
- Return false for arrays containing NaN, Infinity, or -Infinity
- For an empty array, return null
Function Signature
function findLargest(arr) { // Your code here }
Test Cases
- Base cases: empty array [] -> null, single [ x ] -> x
- Mixed arrays: [ 3, 1, 2 ], [ -5, 2, -3, 4 ]
- All negatives: [ -1, -2, -3 ]
- All positives: [ 1, 2, 3 ]
- Decimals: [ -1.5, -0.1, 0, 2.2 ]
- Invalid inputs: null, undefined, 42, '8', {}, () => {}, [1, 'a'], [NaN], [Infinity]
Companies:
tcs
infosys
wipro
capgemini
amazon
Solve Similar questions 🔥
Want to upskill? Explore our courses!
Namaste DSA
Master DSA from scratch with numerous problems, and expert guidance.
Namaste React
Wanna dive deep into React and become Frontend Expert? Learn with me now!
Namaste Frontend System Design
The most comprehensive and detailed course for frontend system design.
Namaste Node.js
Wanna dive deep into Node.js? Enroll into `Namaste Node.js` now!
