πŸ“š

Last Element

Last Element
쉬움 λ°°μ—΄ +10pts

Problem

Write a function that returns the last element of an array.

Examples

Input: last([1, 2, 3])
Output: 3

Explanation

이 λ¬Έμ œλŠ” **λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ μš”μ†Œμ— μ ‘κ·Ό**ν•˜λŠ” 방법을 ν•™μŠ΅ν•©λ‹ˆλ‹€. **λ§ˆμ§€λ§‰ μš”μ†Œ 인덱슀** λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ μΈλ±μŠ€λŠ” `arr.length - 1`μž…λ‹ˆλ‹€: - [1, 2, 3]의 κΈΈμ΄λŠ” 3 - λ§ˆμ§€λ§‰ μΈλ±μŠ€λŠ” 3 - 1 = 2 - arr[2] = 3 **μ™œ length - 1인가?** μΈλ±μŠ€κ°€ 0λΆ€ν„° μ‹œμž‘ν•˜λ―€λ‘œ, 길이가 n인 λ°°μ—΄μ˜ λ§ˆμ§€λ§‰ μΈλ±μŠ€λŠ” n-1μž…λ‹ˆλ‹€. **at(-1) λ©”μ„œλ“œ (ES2022)** `arr.at(-1)`은 λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€. 음수 μΈλ±μŠ€κ°€ μ§€μ›λ©λ‹ˆλ‹€. **빈 λ°°μ—΄ 처리** 빈 λ°°μ—΄μ—μ„œ arr[a...

View detailed explanation β†’

Key Concepts

λ§ˆμ§€λ§‰ μš”μ†Œ μ ‘κ·Ό length 속성 인덱슀 계산 at() λ©”μ„œλ“œ
Time: O(1) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.