📚

First Element

First Element - Explanation

쉬움 배열 O(1) O(1)

Problem Summary

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

Go to Problem →

Detailed Explanation

이 문제는 **배열 인덱싱**의 기초를 학습합니다. 배열의 첫 번째 요소에 접근하는 방법을 배웁니다. **배열 인덱스** 배열의 인덱스는 0부터 시작합니다: - arr[0]: 첫 번째 요소 - arr[1]: 두 번째 요소 - arr[arr.length - 1]: 마지막 요소 **첫 번째 요소 접근** `arr[0]`은 배열의 첫 번째 요소를 반환합니다. **빈 배열의 경우** 빈 배열에서 arr[0]은 undefined를 반환합니다. **at() 메서드 (ES2022)** `arr.at(0)`도 같은 결과를 반환합니다. at()은 음수 인덱스도 지원합니다.

Solution Code

solution.js
function first(arr) {
  return arr[0];
}

Key Concepts from This Problem

1. 배열 인덱싱
2. 0-based 인덱스
3. 요소 접근
4. undefined 반환

Common Mistakes

배열 인덱스는 1이 아닌 0부터 시작합니다
빈 배열에서 접근하면 undefined가 반환됩니다
대괄호 표기법 []을 사용해야 합니다

Hints

Hint 1: 인덱스 0을 사용하세요.

Complexity Analysis

Time Complexity

O(1)

Constant time regardless of input size

Space Complexity

O(1)

Uses almost no additional memory

Related Tags

#배열 #인덱스 #기초