Find the maximum and minimum element in an array

Find the maximum and minimum element in an array

In this article, we will discuss how to find the minimum and maximum elements of an array. To spice things up a little we will take a look at the recursive and functional approach to the problem along with the obvious iterative approach.

Let's get started then.

Iterative Approach

Let's start with finding the maximum element first. We start by initializing a variable let's say max_ele. This element will store the current know maximum value. Right now we can initialize it with the first element of the array. arr[0]

let max_ele = arr[0]

Now, we will iterate over the given array and as we come across the elements, we will check whether they are greater than our existing maximum element(which is the value inside max_ele). If we ever encounter a value greater than our max_ele, we will replace max_ele with this new value.

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

At the end of this loop, max_ele will contain the maximum element in the given array. Before we move forward, try writing a similar code for the minimum element.

Hope you were able to write the code for finding the minimum element because now we will write a function that gives an object with minimum and maximum elements.

function iter_min_max_element(arr) {
  let max_ele = arr[0];
  let min_ele = arr[0];
  for (let i = 1; i < arr.length; i++) {
    if (max_ele < arr[i]) {
      max_ele = arr[i];
    }
    if (min_ele > arr[i]) {
      min_ele = arr[i];
    }
  }
  return { minimum: min_ele, maximum: max_ele };
}
iter_min_max_element([1, 3, 2, 4, 5, -1, 0, 5, 7]);
-> { minimum: -1, maximum: 7 }

The last line returns a JavaScript object. You don't need to dive deep into JS objects for now. It is simply a data structure that allows you to store data in the form of key-value pairs and you can access these values using the following notation obj.minimum and obj.maximum where obj is your object.

Recursive Approach

Now let's check out the recursive approach. We will solve this problem one step at a time.

  1. Figuring out the required parameters:

    • We would need the array arr.

    • A pointer i with default value pointing to the first element.

    • min_ele with value arr[0]

    • max_ele with value arr[0]

    rec_min_max_element(
      arr,
      i = 1,
      min_ele = arr[0],
      max_ele = arr[0]
    ) {}
  1. Deciding the terminal condition: We will stop after reaching the last element of the array.

       if (i >= arr.length) {
         return { minimum: min_ele, maximum: max_ele };
       }
    
  2. Actual logic of finding minimum and maximum elements: We already know this one.

       max_ele = Math.max(max_ele, arr[i]);
       min_ele = Math.min(min_ele, arr[i]);
    
  3. Recursive Call: We will return

    • The array arr

    • The pointer to next index i+1

    • min_ele

    • and max_ele

      return rec_min_max_element(
        arr,
        i + 1,
        min_ele,
        max_ele
      );

And that gives us the following code.

function rec_min_max_element(
  arr,
  i = 1,
  min_ele = arr[0],
  max_ele = arr[0]
) {
  if (i >= arr.length) {
    return { minimum: min_ele, maximum: max_ele };
  }
  max_ele = Math.max(max_ele, arr[i]);
  min_ele = Math.min(min_ele, arr[i]);
  return rec_min_max_element(
    arr,
    i + 1,
    min_ele,
    max_ele
  );
}
rec_min_max_element([1, 3, 2, 4, 5, -1, 0, 5, 7]);
-> { minimum: -1, maximum: 7 }

Functional Approach

Congrats on making it this far. Now it's time for a bonus functional approach! Again you don't need to understand this one. It is just me playing around with the code.

function func_min_max_element(arr) {
  return arr.reduce(
    (acc, curr) => {
      return {
        minimum: Math.min(acc.minimum, curr),
        maximum: Math.max(acc.maximum, curr),
      };
    },
    {
      minimum: arr[0],
      maximum: arr[0],
    }
  );
}
func_min_max_element([1, 3, 2, 4, 5, -1, 0, 5, 7]);
-> { minimum: -1, maximum: 7 }

And that is it for this post. GG if you were able to make it this far. Next time we will work on finding the kth maximum element of an array.