Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to Get Index of Greatest Value in an Array in JavaScript...

How to Get Index of Greatest Value in an Array in JavaScript ?

In this article, we will see how to return the index for the greatest value in an array with the help of JavaScript. To return the index of the greatest value in an array using JavaScript, the following approaches will be used:

Some of the examples are to return the index of the greatest value in an array.

Example: The greatest value in the array is 8, which is located at index 3. 

Input: arr = [2, 5, 1, 8, 3]; 
Output: 3

 

Example: The greatest value in the array is -1, which is located at index 2.

Input: arr =[-2, -5, -1, -8, -3];
Output: 2

Using for loop: We can use a simple for loop to iterate through the array and keep track of the index of the greatest value seen so far.

Approach:

  • We declare a function that takes an array arr as a parameter.
  • We initialize a variable maxIndex to 0, which will hold the index of the greatest value.
  • We loop through the array using a for loop, starting at index 1 (We have already set maxIndex to 0).
  • For each element in the array, we check if it is greater than the current maximum value (the value at arr[maxIndex]).
  • If it is greater, we update the maxIndex variable to the current index i.
  • We return maxIndex after the loop.

Example 1: In this example, the greatest value in the array is 8, which is located at index 3.

Javascript




function indexOfMax(arr) {
    let maxIndex = 0;
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] > arr[maxIndex]) {
            maxIndex = i;
        }
    }
    return maxIndex;
}
  
let arr = [2, 5, 1, 8, 3];
console.log(indexOfMax(arr));


Output:

3

Example 2: In this example, the greatest value in the array is -1, which is located at index 2.

Javascript




function indexOfMax(arr) {
    let maxIndex = 0;
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] > arr[maxIndex]) {
            maxIndex = i;
        }
    }
    return maxIndex;
}
  
let arr = [-2, -5, -1, -8, -3];
console.log(indexOfMax(arr));


Output:

2

Using the reduce() method: We can use the reduce() method to find the index of the greatest value in an array.

Approach:

  • We declare a function that takes an array arr as a parameter.
  • We call the reduce() method on the array arr.
  • The reduce() method takes a callback function with four parameters:
  • maxIndex is the index of the current maximum value seen so far (initialized to 0).
  • elem is the current element being processed.
  • i  is the index of the current element being processed.
  • arr is the original array being processed.
  • In the callback function, we check if the current element elem is greater than the current maximum value (arr[maxIndex]).
  • If it is greater, we return the current index i, which becomes the new maxIndex.
  • If it is not greater, we return the maxIndex variable.
  • We return the final maxIndex after the function call.

Example 3: In this example, the greatest value in the array is 8, which is located at index 3.

Javascript




function indexOfMax(arr) {
    return arr.reduce((maxIndex, elem, i, arr) => 
        elem > arr[maxIndex] ? i : maxIndex, 0);
}
  
let arr = [2, 5, 1, 8, 3];
console.log(indexOfMax(arr));


Output:

3 

Example 4: In this example, the greatest value in the array is -1, which is located at index 2.

Javascript




function indexOfMax(arr) {
    return arr.reduce((maxIndex, elem, i, arr) => 
        elem > arr[maxIndex] ? i : maxIndex, 0);
let arr = [-2, -5, -1, -8, -3];
console.log(indexOfMax(arr));


Output:

2

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments