Find the largest number in array

medium

By - Aman Pareek

Last Updated - 27/08/2024

Problem Statement

Given an array of numbers, you need to find the largest number in the array. The function should handle various scenarios, including arrays with negative numbers, floating-point numbers, and edge cases like empty arrays or invalid inputs.

Example 1

Input: array = [1, 5, 3, 7, 2]

Output: 7

Example 2

Input: array = []

Output: Throw error => Input is not a valid array or array is empty

Example 3

Input: string = "aman"

Output: Throw error => Input is not a valid array or array is empty

Example 4

Input: array = [-5, 8, 9, 20]

Output: 20

Solution 1: Using Math.max with Spread Operator

function findLargestNumber(nums) {
    if (!Array.isArray(nums) || nums.length === 0) {
        throw new Error('Input is not a valid array or array is empty');
    }
    return Math.max(...nums);
} 

const array1 = [1, 5, 3, 7, 2];
findLargestNumber(array1);  //output: 7 

const array2 = [];
findLargestNumber(array2);  //Throw error => Input is not a valid array or array is empty 

const string3 = "aman";
findLargestNumber(string3);  //Throw error => Input is not a valid array or array is empty 

const array4 = [-5, 8, 9, 20];
findLargestNumber(array4);  //output: 20 

Solution 2: Using reduce Method

function findLargestNumberReduce(nums) {
    if (!Array.isArray(nums) || nums.length === 0) {
        throw new Error('Input is not a valid array or array is empty');
    }
    return nums.reduce((max, current) => {
        if (current > max) {
            return current;
        }
        return max;
    }, nums[0]);
} 

const array1 = [1, 5, 3, 7, 2];
findLargestNumberReduce(array1);  //output: 7 

const array2 = [];
findLargestNumberReduce(array2);  //Throw error => Input is not a valid array or array is empty 

const string3 = "aman";
findLargestNumberReduce(string3);  //Throw error => Input is not a valid array or array is empty 

const array4 = [-5, 8, 9, 20];
findLargestNumberReduce(array4);  //output: 20 

Solution 3: Using a Loop

function findLargestNumberLoop(nums) {
    if (!Array.isArray(nums) || nums.length === 0) {
        throw new Error('Input is not a valid array or array is empty');
    }
    let largest = nums[0];
    for (let i = 1; i < nums.length; i++) {
        if (nums[i] > largest) {
            largest = nums[i];
        }
    }
    return largest;
} 

const array1 = [1, 5, 3, 7, 2];
findLargestNumberLoop(array1);  //output: 7 

const array2 = [];
findLargestNumberLoop(array2);  //Throw error => Input is not a valid array or array is empty 

const string3 = "aman";
findLargestNumberLoop(string3);  //Throw error => Input is not a valid array or array is empty 

const array4 = [-5, 8, 9, 20];
findLargestNumberLoop(array4);  //output: 20 

Solution 4: Using forEach Method

function findLargestNumberForEach(nums) {
    if (!Array.isArray(nums) || nums.length === 0) {
        throw new Error('Input is not a valid array or array is empty');
    }
    let largest = nums[0];
    nums.forEach(num => {
        if (num > largest) {
            largest = num;
        }
    });
    return largest;
} 

const array1 = [1, 5, 3, 7, 2];
findLargestNumberForEach(array1);  //output: 7 

const array2 = [];
findLargestNumberForEach(array2);  //Throw error => Input is not a valid array or array is empty 

const string3 = "aman";
findLargestNumberForEach(string3);  //Throw error => Input is not a valid array or array is empty 

const array4 = [-5, 8, 9, 20];
findLargestNumberForEach(array4);  //output: 20 

Solution 5: Using Array.prototype.sort

function findLargestNumberSort(nums) {
    if (!Array.isArray(nums) || nums.length === 0) {
        throw new Error('Input is not a valid array or array is empty');
    }
    return nums.slice().sort((a, b) => b - a)[0];
} 

const array1 = [1, 5, 3, 7, 2];
findLargestNumberSort(array1);  //output: 7 

const array2 = [];
findLargestNumberSort(array2);  //Throw error => Input is not a valid array or array is empty 

const string3 = "aman";
findLargestNumberSort(string3);  //Throw error => Input is not a valid array or array is empty 

const array4 = [-5, 8, 9, 20];
findLargestNumberSort(array4);  //output: 20 

Solution 6: Using Math.max with apply

function findLargestNumberApply(nums) {
    if (!Array.isArray(nums) || nums.length === 0) {
        throw new Error('Input is not a valid array or array is empty');
    }
    return Math.max.apply(null, nums);
} 

const array1 = [1, 5, 3, 7, 2];
findLargestNumberApply(array1);  //output: 7 

const array2 = [];
findLargestNumberApply(array2);  //Throw error => Input is not a valid array or array is empty 

const string3 = "aman";
findLargestNumberApply(string3);  //Throw error => Input is not a valid array or array is empty 

const array4 = [-5, 8, 9, 20];
findLargestNumberApply(array4);  //output: 20 

Solution 7: Using for...of Loop

function findLargestNumberForOf(nums) {
    if (!Array.isArray(nums) || nums.length === 0) {
        throw new Error('Input is not a valid array or array is empty');
    }
    let largest = nums[0];
    for (const num of nums) {
        if (num > largest) {
            largest = num;
        }
    }
    return largest;
} 

const array1 = [1, 5, 3, 7, 2];
findLargestNumberForOf(array1);  //output: 7 

const array2 = [];
findLargestNumberForOf(array2);  //Throw error => Input is not a valid array or array is empty 

const string3 = "aman";
findLargestNumberForOf(string3);  //Throw error => Input is not a valid array or array is empty 

const array4 = [-5, 8, 9, 20];
findLargestNumberForOf(array4);  //output: 20 

Solution 8: Using while Loop

function findLargestNumberWhile(nums) {
    if (!Array.isArray(nums) || nums.length === 0) {
        throw new Error('Input is not a valid array or array is empty');
    }
    let largest = nums[0];
    let index = 1;
    while (index < nums.length) {
        if (nums[index] > largest) {
            largest = nums[index];
        }
        index++;
    }
    return largest;
} 

const array1 = [1, 5, 3, 7, 2];
findLargestNumberWhile(array1);  //output: 7 

const array2 = [];
findLargestNumberWhile(array2);  //Throw error => Input is not a valid array or array is empty 

const string3 = "aman";
findLargestNumberWhile(string3);  //Throw error => Input is not a valid array or array is empty 

const array4 = [-5, 8, 9, 20];
findLargestNumberWhile(array4);  //output: 20 

Solution 9: Using find Method

function findLargestNumberFind(nums) {
    if (!Array.isArray(nums) || nums.length === 0) {
        throw new Error('Input is not a valid array or array is empty');
    }
    let largest = nums[0];
    nums.find(num => {
        if (num > largest) {
            largest = num;
        }
        return false;
    });
    return largest;
} 

const array1 = [1, 5, 3, 7, 2];
findLargestNumberFind(array1);  //output: 7 

const array2 = [];
findLargestNumberFind(array2);  //Throw error => Input is not a valid array or array is empty 

const string3 = "aman";
findLargestNumberFind(string3);  //Throw error => Input is not a valid array or array is empty 

const array4 = [-5, 8, 9, 20];
findLargestNumberFind(array4);  //output: 20 

Solution 10: Using map and reduce Together

function findLargestNumberMapReduce(nums) {
    if (!Array.isArray(nums) || nums.length === 0) {
        throw new Error('Input is not a valid array or array is empty');
    }
    return nums.map(num => num).reduce((max, current) => current > max ? current : max, nums[0]);
} 

const array1 = [1, 5, 3, 7, 2];
findLargestNumberMapReduce(array1);  //output: 7 

const array2 = [];
findLargestNumberMapReduce(array2);  //Throw error => Input is not a valid array or array is empty 

const string3 = "aman";
findLargestNumberMapReduce(string3);  //Throw error => Input is not a valid array or array is empty 

const array4 = [-5, 8, 9, 20];
findLargestNumberMapReduce(array4);  //output: 20