

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the largest and smallest number in an unsorted array of integers in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers. The function should, in linear time and constant space, find the largest and smallest numbers that exist in the array. The function should return an object that contains the min and max numbers.
Example
Following is the code −
const arr = [112, 24, 31, 44, 101, 203, 33, 56]; const findMaxMin = (arr) => { let max = arr[0]; let min = arr[0]; for(let i = 0; i < arr.length; i++) { if(arr[i] > max) { max = arr[i]; } else if (arr[i] < min) { min = arr[i]; } }; return { min, max }; }; console.log(findMaxMin(arr));
Output
Following is the output on console −
{ min: 24, max: 203 }
- Related Questions & Answers
- Finding the largest non-repeating number in an array in JavaScript
- Find the difference of largest and the smallest number in an array without sorting it in JavaScript
- K’th Smallest/Largest Element in Unsorted Array in C++
- kth smallest/largest in a small range unsorted array in C++
- Finding the smallest fitting number in JavaScript
- JavaScript Recursion finding the smallest number?
- Finding difference of greatest and the smallest digit in a number - JavaScript
- Find the largest pair sum in an unsorted array in C++
- Finding the Largest Triple Product Array in JavaScript
- Finding smallest number using recursion in JavaScript
- Finding the largest prime factor of a number in JavaScript
- Finding the smallest positive integer not present in an array in JavaScript
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Finding unlike number in an array - JavaScript
- Difference between the largest and the smallest primes in an array in Java
Advertisements