Find a maximum and minimum element in an array.

In the following code sample, we will see how to find the maximum and minimum elements in an array. Let’s start:

Example

Input
Input array: 10,20,50,2,30

Output:
Maximum  = 50
Minimum = 2

Program

function maximumAndMinimumElement(arr) {

    //Assume first element of array is max and minimum
    let max = arr[0];
    let min = arr[0];

    for(let i = 0; i < arr.length; i++) {

        //If current element is greater than maximum
        if(arr[i] > max) {
            max = arr[i];
        }

        //If current element is less than minimum
        if(arr[i] < min) {
            min = arr[i];
        }
    }

    console.log('Maximum  = ' + max);
    console.log('Minimum = ' + min);
}


maximumAndMinimumElement([10,20,50,2,30]); // BigO of this program is: O(n)

Leave a Reply

Your email address will not be published.