Return an array of all the indices of minimum elements in the array in JavaScript

JavascriptWeb DevelopmentFront End Technology

An Array in JavaScript is a single variable where it can store different elements. These elements are stored at contiguous memory locations. Array elements can be accessed with the help of index numbers. The index numbers will start from 0.

Syntax

Below is the basic declaration of the array in JavaScript −

Cosnt cars = [Maruti, Hyundai, Honda];

We need to return an array of all the indices of minimum elements in a JavaScript array

Let’s look into the input-output scenario

Assume there is an integer array where the minimum element is repeated more than once. We need to get the index numbers of the minimum element in the array.

Array = [10, 22, 30, 44, 10, 22, 30, 10, 10];
Output = [0, 4, 7, 8]

Using Math.min() function

One way to perform the above task is by using the Math.min() function, spread(…) operator, and forEach method.

Math.min() is a static and built-in function in JavaScript that accepts a series of values as a parameter and returns the minimum value from the given series.

<script>
   document.write(Math.min(23, 32, 441));
</script>

Since 23 is the lowest value of the given values, this snippet prints the value 23 (on the browser).

But, the problem with this is, if you try to pass an array as a parameter to the math.min() method as shown below it will return NaN value.

<script>
   const array1 = [2, 3, 1];
   document.write(Math.min(array1));
</script>

We can resolve this with the help of the spread(…) operator. Using the spread operator, we can expand the elements of an array or string as a series of values.

  • If you pass an array of integers to the math.min() method using the spread operator, returns the minimum value of the given array.

  • The forEach() method of JavaScript allows you to perform a particular operation on each element in the current array.

  • To get the index number(s) of minimum value(s); Compare each element in the array with minimum value using forEach() method. In case of a match, print the index of the matched element.

Example

Following is the complete example which returns the indices of minimum values of an array −

<!DOCTYPE html> <html> <head> <title>Indexes if minimum elements in an array</title> </head> <body> <p id="para"> </p> <h2 id="demo"> </h2> <script> var string = ""; const array = [10, 20, 30, 10, 50, 10, 60, 70]; array.forEach(func); document.getElementById("para").innerHTML ="The indices of the elements present in array are: " + "<br>" +string; function func(curr_item, index_num) { string += index_num + ": " + curr_item + "<br>"; } function MinIndices(array) { var minimum = Math.min(...array); var Min_index = []; array.forEach(function(curr_item, index) { if (curr_item === minimum) { Min_index.push(index); } }); return Min_index; } document.getElementById("demo").innerHTML = "The indices of minimum elements in array are: " + MinIndices(array); </script> </body </html>

As we observe the minimum value in the input array of the above program is 10 and the program prints the indices where of the elements whose value is 10.

Using the apply() method

We can also return indices of minimum elements in an array by using Math.min() function, apply() method, and forEach method.

Example

Below is the following example by using apply() method, forEach method and Math.min() function −

<!DOCTYPE html> <html> <head> <title>Indexes if minimum elements in an array</title> </head> <body> <p id="para"> </p> <h2 id="demo"> </h2> <script> var string = ""; const array = [1, 8, 9, 5, 23, 1, 34, 8, 5, 1, 9]; array.forEach(func); document.getElementById("para").innerHTML ="The indices of the elements present in array are: " + "<br>" +string; function func(curr_item, index_num) { string += index_num + ": " + curr_item + "<br>"; } function MinIndices(array) { var minimum = Math.min.apply(null, array); var Min_index = []; array.forEach(function(curr_item, index) { if (curr_item === minimum) { Min_index.push(index); } }); return Min_index; } document.getElementById("demo").innerHTML = "The indices of minimum elements in array are: " + MinIndices(array); </script> </body> </html>

In the below output, the minimum element is 1 and it is at three indices in the array. Therefore, it prints the indices where 1 exists in the array.

Using reduce() method

The reduce() method accepts a callback function(reducer) as a parameter and applies this function to every element of the current array. Usually, the result of this will be a single value.

Below given code demonstrates the reduce() method, in here we are using it to multiply all the elements of an array and return the result.

const myArray = [21, 244, 3009, 4221];
const mul = myArray.reduce((pre, curr) => pre * curr, 1);
console.log(mul);

The result of the above snippet will be 65079867636.

Example

The following example returns an array of all the indices of minimum elements in the input array using reduce() method.

Here, we are passing Math.min() method as the operation of the reducer() so that it will retrieve the minimum value of the elements in the current array.

<!DOCTYPE html> <html> <head> <title>Indexes if minimum elements in an array</title> </head> <body> <p id="para"> </p> <h2 id="demo"> </h2> <script> var string = ""; const array = [0, 7, 9, 10, 17, 18, 33, 45, 54, 99, 83, 0]; array.forEach(func); document.getElementById("para").innerHTML ="The indices of the elements present in array are: " + "<br>" +string; function func(curr_item, index_num) { string += index_num + ": " + curr_item + "<br>"; } const MinIndices = array => { const minimum = array.reduce((acc, val) => Math.min(acc, val), Infinity); const res = []; let i = 0; let len = array.length; for(i; i < len; i++){ if(array[i] !== minimum){ continue; }; res.push(i); }; return res; }; document.getElementById("demo").innerHTML ="The indices of minimum elements in array are: " + MinIndices(array); </script> </body> </html>
raja
Updated on 22-Sep-2022 11:59:23

Advertisements