We have an array of Numbers, and we are required to write a function that returns the second smallest value from the array.
For example − if the array is −
const arr = [67, 87, 56, 8, 56, 78, 54, 67, 98, 56, 54];
Then the output should be the following −
54
because 54 is the smallest value after 8
Example
const arr = [67, 87, 56, 8, 56, 78, 54, 67, 98, 56, 54];
const minimumIndex = arr => {
return arr.indexOf(Math.min(...arr));
};
const secondMinimum = arr => {
const copy = arr.slice();
copy.splice(minimumIndex(copy), 1);
return copy[minimumIndex(copy)];
};
console.log(secondMinimum(arr));Output
The output in the console will be −
54