[프로그래머스 lev1/JS] 제일 작은 수 제거하기

woolee의 기록보관소·2022년 11월 3일
0

알고리즘 문제풀이

목록 보기
36/178

문제 출처

프로그래머스 lev1 - 제일 작은 수 제거하기

문제

나의 풀이

function solution(arr) {
  return arr.length === 1 ? [-1] : arr.filter(el => el > Math.min(...arr))
}

console.log(solution([4, 3, 2, 1]));

다른 풀이

apply 메서드를 사용한 최소값 찾기

function solution(arr) {
  if (arr.length === 1 ) {
      return [-1]
  }
  const minValue = Math.min.apply(null, arr)
  const index = arr.findIndex(value => value === minValue)
  arr.splice(index, 1)
  return arr
}

console.log(solution([4, 3, 2, 1]));
profile
https://medium.com/@wooleejaan

0개의 댓글