π
Last Element
Last Elementμ¬μ λ°°μ΄ +10pts
Problem
Write a function that returns the last element of an array.
Examples
Input:
last([1, 2, 3])Output:
3Explanation
μ΄ λ¬Έμ λ **λ°°μ΄μ λ§μ§λ§ μμμ μ κ·Ό**νλ λ°©λ²μ νμ΅ν©λλ€. **λ§μ§λ§ μμ μΈλ±μ€** λ°°μ΄μ λ§μ§λ§ μΈλ±μ€λ `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.