Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Return an array of all the indices of minimum elements in the array in JavaScript
In JavaScript arrays, you might need to find all indices where the minimum value appears. This is useful when the smallest element occurs multiple times and you want to locate all positions.
Problem Statement
Given an array with duplicate minimum values, we need to return an array containing all indices of these minimum elements.
Input: [10, 22, 30, 44, 10, 22, 30, 10, 10] Output: [0, 4, 7, 8]
Here, 10 is the minimum value appearing at indices 0, 4, 7, and 8.
Using Math.min() with Spread Operator
Math.min() returns the smallest value from a series of numbers. When combined with the spread operator (...), it can find the minimum value in an array. We then use forEach() to find all matching indices.
<!DOCTYPE html>
<html>
<head>
<title>Find Minimum Element Indices</title>
</head>
<body>
<p id="arrayDisplay"></p>
<p id="result"></p>
<script>
const array = [10, 20, 30, 10, 50, 10, 60, 70];
// Display original array
document.getElementById("arrayDisplay").innerHTML = "Array: " + array.join(", ");
function findMinIndices(arr) {
var minimum = Math.min(...arr);
var minIndices = [];
arr.forEach(function(element, index) {
if (element === minimum) {
minIndices.push(index);
}
});
return minIndices;
}
const indices = findMinIndices(array);
document.getElementById("result").innerHTML =
"Minimum value: " + Math.min(...array) + "<br>" +
"Indices of minimum elements: " + indices.join(", ");
</script>
</body>
</html>
Array: 10, 20, 30, 10, 50, 10, 60, 70 Minimum value: 10 Indices of minimum elements: 0, 3, 5
Using Math.min.apply() Method
An alternative approach uses Math.min.apply() instead of the spread operator to find the minimum value.
<!DOCTYPE html>
<html>
<head>
<title>Find Minimum Indices with apply()</title>
</head>
<body>
<p id="arrayInfo"></p>
<p id="output"></p>
<script>
const array = [1, 8, 9, 5, 23, 1, 34, 8, 5, 1, 9];
document.getElementById("arrayInfo").innerHTML = "Array: " + array.join(", ");
function findMinIndicesWithApply(arr) {
var minimum = Math.min.apply(null, arr);
var minIndices = [];
arr.forEach(function(element, index) {
if (element === minimum) {
minIndices.push(index);
}
});
return minIndices;
}
const result = findMinIndicesWithApply(array);
document.getElementById("output").innerHTML =
"Minimum value: " + Math.min.apply(null, array) + "<br>" +
"Indices: " + result.join(", ");
</script>
</body>
</html>
Array: 1, 8, 9, 5, 23, 1, 34, 8, 5, 1, 9 Minimum value: 1 Indices: 0, 5, 9
Using reduce() Method
The reduce() method can also find the minimum value by comparing each element with the accumulator.
<!DOCTYPE html>
<html>
<head>
<title>Find Minimum Indices with reduce()</title>
</head>
<body>
<p id="arrayData"></p>
<p id="finalResult"></p>
<script>
const array = [0, 7, 9, 10, 17, 18, 33, 45, 54, 99, 83, 0];
document.getElementById("arrayData").innerHTML = "Array: " + array.join(", ");
const findMinIndices = arr => {
const minimum = arr.reduce((acc, val) => Math.min(acc, val), Infinity);
const indices = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === minimum) {
indices.push(i);
}
}
return indices;
};
const minIndices = findMinIndices(array);
document.getElementById("finalResult").innerHTML =
"Minimum value: " + array.reduce((acc, val) => Math.min(acc, val), Infinity) + "<br>" +
"Indices: " + minIndices.join(", ");
</script>
</body>
</html>
Array: 0, 7, 9, 10, 17, 18, 33, 45, 54, 99, 83, 0 Minimum value: 0 Indices: 0, 11
Comparison of Methods
| Method | Browser Support | Performance | Readability |
|---|---|---|---|
| Math.min(...array) | ES6+ | Good | Excellent |
| Math.min.apply() | All browsers | Good | Good |
| reduce() | ES5+ | Fair | Good |
Conclusion
The spread operator with Math.min() provides the most readable solution for finding all indices of minimum elements in an array. For older browser support, use Math.min.apply() as an alternative.
