Skip to content

Instantly share code, notes, and snippets.

@leeyspaul
Last active May 5, 2022 10:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leeyspaul/105970b47386942e52cfd8d5e272a2e3 to your computer and use it in GitHub Desktop.
Save leeyspaul/105970b47386942e52cfd8d5e272a2e3 to your computer and use it in GitHub Desktop.
Codewars Challenge: "Remove the Minimum" by user:bkaes (https://www.codewars.com/users/bkaes)

My solution

function removeSmallest(numbers) {
  // First step is to remove the smallest value given array
  // Filter the smallest number out with Math.min.apply
  let smallestNumber = Math.min.apply(Math,numbers);
  
  // Now remove the number from the array 
  // Find the index of smallest number
  let indexOfSmallestNumber = numbers.indexOf(smallestNumber);
  // Remove the smallest number with splice method 
  numbers.splice(indexOfSmallestNumber,1);

  return numbers;
}

Explanation

This challenge asked to remove the smallest value given in an array of numbers. The first step was to find the smallest number in the array which was achieved by using the Math.min function which defined by MDN is:

"The Math.min() function returns the smallest of zero or more numbers."

But because the Math.min() function only takes number values, the .apply() had to be used to call the action on the given numbers array. Hence, Math.min.apply(Math, numbers).

After finding the smallest number the next step was to remove that number from the existing numbers array. This was done using the "splice method" .

The splice method alters an array by removing or adding elements given index numbers. Using the .indexOf() method to find the index of the smallest number was the first step. After finding the index number, .splice() was then used to remove the mentioned number and afterwards the original numbers array was returned.

/* The problem:
Task
Given an array of integers, remove the smallest value. Do not mutate the original array/list. If there are multiple elements with the same value, remove the one with a lower index. If you get an empty array/list, return an empty array/list.
Don't change the order of the elements that are left.
*/
// My solution
function removeSmallest(numbers) {
// First step is to remove the smallest value given array
// Filter the smallest number out with Math.min.apply
let smallestNumber = Math.min.apply(Math,numbers);
// Now remove the number from the array
// Find the index of smallest number
let indexOfSmallestNumber = numbers.indexOf(smallestNumber);
// Remove the smallest number with splice method
numbers.splice(indexOfSmallestNumber,1);
return numbers;
}
// The "best" solution
function removeSmallest(numbers) {
if(!numbers)return [];
var min=Math.min.apply(null,numbers);
numbers.splice(numbers.indexOf(min),1);
return numbers;
}
@Rb-wahid
Copy link

Thanks for your detailed explanation.

@recscse
Copy link

recscse commented Jul 26, 2021

the solution is right but it mutates the original array and according to the questions we can not mutate the original array

@dedMikola
Copy link

function removeSmallest(numbers) {
if(!numbers)return [];
let min=Math.min.apply(null,numbers);
let newArr = numbers.slice(0);
newArr.splice(newArr.indexOf(min),1);
return newArr;
}

@darksbond
Copy link

this works. thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment