Published on January 26, 2024By DeveloperBreeze


// Function to find the maximum element in an array
function findMaxElement(arr) {
    if (arr.length === 0) {
        return "Array is empty";
    }

    let max = arr[0];
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    return max;
}

// Function to find the minimum element in an array
function findMinElement(arr) {
    if (arr.length === 0) {
        return "Array is empty";
    }

    let min = arr[0];
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
    }

    return min;
}

// Example usage
const myArray = [3, 8, 2, 7, 5, 1, 4, 6];
const maxElement = findMaxElement(myArray);
const minElement = findMinElement(myArray);

console.log("Maximum Element:", maxElement);
console.log("Minimum Element:", minElement);

Comments

Please log in to leave a comment.

Continue Reading:

Find Maximum Value in a List

Published on January 26, 2024

python