알고리즘 81 - Remove the minimum

jabae·2021년 11월 1일
0

알고리즘

목록 보기
81/97

Q.

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

removeSmallest([1,2,3,4,5]) = [2,3,4,5]
removeSmallest([5,3,2,1,4]) = [5,3,2,4]
removeSmallest([2,2,1,2,1]) = [2,2,2,1]

A)

function removeSmallest(numbers) {
  let min = numbers.indexOf(Math.min(...numbers));

  return numbers.filter((d, i) => i !== min);
}

고차함수 녀석들은 참 유용한 것 같다... 요소로 접근해도 되고,인덱스로도 가능하고.. 정말 똑똑해! 😆

profile
it's me!:)

0개의 댓글