Here is a simple method...
const numbers = Array
.from(
{ length: 50 },
() => parseInt(Math.random() * 100 + 1),
)
.sort((a, b) => a - b);
// Modify the `numbers` array, removing all instances equal to the smallest value.
numbers.splice(0, numbers.lastIndexOf(numbers[0]) + 1);
Evidence for the above procedure ...
const numbers = Array
.from(
{ length: 50 },
() => parseInt(Math.random() * 100 + 1),
)
.sort((a, b) => a - b);
console.log({
arrayLength: numbers.length,
minValue: numbers[0],
sortedNumbers: numbers,
});
// Modify the `numbers` array, removing all instances equal to the smallest value.
numbers.splice(0, numbers.lastIndexOf(numbers[0]) + 1);
console.log({
arrayLength: numbers.length,
shortenedArray: numbers,
});
.as-console-wrapper { min-height: 100%!important; top: 0; }
Similar tasks that alter an array by actively slicing out elements from it may benefit from a more generic reject
based approach ...
function reject(arr, condition, target) {
const rejectedItems = [];
let index = arr.length;
const copyArr = [...arr];
while (index) {
if (
// Consider *sparse arrays*.
arr.hasOwnProperty(--index) &&
// [element, index, copy] called within `target` context.
condition.call((target ?? null), copyArr[index], index, copyArr)
) {
rejectedItems.unshift(arr.splice(index, 1)[0]);
}
}
return rejectedItems;
}
const numberArr = Array
.from(
{ length: 50 },
() => parseInt(Math.random() * 100 + 1),
);
const minVal = Math.min(...numberArr);
console.log({
arrayLength: numberArr.length,
minVal,
numberArr,
});
console.log({
rejectedItems: reject(numberArr, val => val === minVal),
arrayLength: numberArr.length,
sortedNumbers: numberArr.sort((a, b) => a - b),
});
.as-console-wrapper { min-height: 100%!important; top: 0; }