Delete smallest number in JS array

I have an array of numbers [2, 1, 3, 4, 5, 1] ​​and want to remove the smallest number in the list. But for some reason, my IF statement is skipped.

I checked on my own the "numbers [i + 1]" and "numbers [i]" work, but the "numbers [i + 1] <numbers [i]" do not ...

function removeSmallest(numbers) { var smallestNumberKEY = 0; for (i = 0; i <= numbers.lenths; i++) { if (numbers[i + 1] < numbers[i]) { smallestNumberKEY = i + 1; } } numbers.splice(smallestNumberKEY, 1); return numbers; } document.write(removeSmallest([2, 1, 3, 4, 5, 1])); 
+8
source share
8 answers

You have a typo in your code, the array has no lenths property

 function removeSmallest(numbers) { var smallestNumberKEY = 0; for (var i = 0; i < numbers.length - 1; i++) { if (numbers[i + 1] < numbers[i]) { smallestNumberKEY = i + 1; numbers.splice(smallestNumberKEY, 1); } } return numbers; } document.write(removeSmallest([2, 1, 3, 4, 5, 1])); 

But your algorithm will not work for another array, for example [5, 3, 1, 4, 1] , it will also remove the value 3 .

You can find the min value using the Math.min function and then filter the array

 function removeSmallest(arr) { var min = Math.min(...arr); return arr.filter(e => e != min); } 
+4
source

You can use Array#filter instead

 function removeSmallest(arr) { var min = Math.min.apply(null, arr); return arr.filter((e) => {return e != min}); } console.log(removeSmallest([2, 1, 3, 4, 5, 1])) 
+3
source

A “short” solution using the Array.forEach and Array.splice :

 function removeSmallest(numbers) { var min = Math.min.apply(null, numbers); numbers.forEach((v, k, arr) => v !== min || arr.splice(k,1)); return numbers; } console.log(removeSmallest([2, 1, 3, 4, 5, 1])); // [2, 3, 4, 5] 
+1
source

This sentence is with a single Array#reduce loop and without Math.min .

The algorithm sets the element value in the first min loop and returns an empty array, because the actual element is the smallest value, and the result set should not contain the smallest value.

The next cycle may have

  • the value is less than min , then assign a min and return a copy of the original array to the previous element, since a new minimum has been found and all the other previous elements are larger than the actual value and belong to the result array.
  • value is greater than min , then the actual value will be transferred to the result set.
  • value equal to min , then vaue is skipped.

 'use strict'; var removeSmallest = function () { var min; return function (r, a, i, aa) { if (!i || a < min) { min = a; return aa.slice(0, i); } if (a > min) { r.push(a); } return r; } }(); document.write('<pre>' + JSON.stringify([2, 1, 3, 2, 4, 5, 1].reduce(removeSmallest, []), 0, 4) + '</pre>'); 
+1
source

I would rather do the job. The Spread statement is very convenient when a Math object encounters an Array object.

 var ar = [2, 1, 3, 4, 5, 1]; ar.reduce((p,c,i,a) => (p == c && a.splice(i,1),p) , Math.min(...ar)); document.write(JSON.stringify(ar)); 
+1
source

another solution with splice and indexOf :

 array = [2, 1, 3, 4, 5, 1]; function replace(arr){ arr = arr.slice(); //copy the array arr.splice( arr.indexOf(Math.min.apply(null, arr)),1) return arr; } document.write( replace(array) ,'<br> original array : ', array) 

edit: creating a copy of the array will avoid changing the original array

0
source

I like this oneliner: list.filter(function(n) { return n != Math.min.apply( Math, list ) })

check here: https://jsfiddle.net/rz2n4rsd/1/

 function remove_smallest(list) { return list.filter(function(n) { return n != Math.min.apply( Math, list ) }) } var list = [2, 1, 0, 4, 5, 1] console.log(list) // [2, 1, 0, 4, 5, 1] list = remove_smallest(list) console.log(list) // [2, 1, 4, 5, 1] list = remove_smallest(list) console.log(list) // [2, 4, 5] 
0
source

I had to do this, but I needed a solution that would not change the numbers input array and was executed in O (n) time. If this is what you are looking for, try this:

 const removeSmallest = (numbers) => { const minValIndex = numbers.reduce((finalIndex, currentVal, currentIndex, array) => { return array[currentIndex] <= array[finalIndex] ? currentIndex : finalIndex }, 0) return numbers.slice(0, minValIndex).concat(numbers.slice(minValIndex + 1)) } 
0
source

All Articles