본문 바로가기
알고리즘/Codewars

[Codewars] Remove the minimum (7 kyu) / JavaScript

by fluss 2023. 2. 17.

https://www.codewars.com/kata/563cf89eb4747c5fb100001b

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

The museum of incredible dull things

The museum of incredible dull things wants to get rid of some exhibitions. Miriam, the interior architect, comes up with a plan to remove the most boring exhibitions. She gives them a rating, and then removes the one with the lowest rating.

 

However, just as she finished rating all exhibitions, she's off to an important fair, so she asks you to write a program that tells her the ratings of the items after one removed the lowest one. Fair enough.

 

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.

 

Examples

* Input: [1,2,3,4,5], output = [2,3,4,5]
* Input: [5,3,2,1,4], output = [5,3,2,4]
* Input: [2,2,1,2,1], output = [2,2,2,1]
 

코드

function removeSmallest(numbers) {
  let min = Math.min(...numbers);
  let minIndex = numbers.indexOf(min);
  let result = [];
  for(let i = 0; i < numbers.length; i++){
    if(i !== minIndex) result.push(numbers[i]);
  }
  return result;
}

 

댓글