Find Minimum
Find Minimum - Explanation
Problem Summary
Write a function that returns the smallest number in an array.
Go to Problem →Detailed Explanation
이 문제는 **Math.min()**과 **스프레드 연산자**를 사용하여 배열에서 최소값을 찾는 방법을 학습합니다. **Math.min() 함수** Math.min()은 전달된 인수들 중 가장 작은 값을 반환합니다: `Math.min(3, 1, 4)` → 1 **스프레드 연산자 (...)** 배열을 개별 인수로 펼쳐줍니다: `Math.min(...[3, 1, 4])`는 `Math.min(3, 1, 4)`와 같습니다. **음수 배열 처리** [-5, -1, -10]에서 -10이 가장 작습니다. Math.min은 음수도 올바르게 처리합니다. **reduce로 구현하기** ```javascript function findMin(arr) { return arr.reduce((min, cur) => cur < min ? cur : min, arr[0]); } ``` **빈 배열 주의** `Math.min(...[])`은 Infinity를 반환합니다. 실제 사용 시 빈 배열 체크가 필요할 수 있습니다. **최대값 vs 최소값** - 최대값: `Math.max(...arr)` - 최소값: `Math.min(...arr)` 두 함수의 사용법은 동일하며, 반환하는 값만 다릅니다.
Solution Code
function findMin(arr) {
return Math.min(...arr);
}Key Concepts from This Problem
Common Mistakes
Hints
Complexity Analysis
Time Complexity
O(n)
Grows linearly with input size
Space Complexity
O(n)
Uses memory proportional to input size