📚
Array Includes
Array Includes - Explanation
쉬움 배열 O(n) O(1)
Problem Summary
Write a function that checks if an array includes a specific value.
Go to Problem →Detailed Explanation
이 문제는 **includes 메서드**를 사용하여 배열에 특정 값이 포함되어 있는지 확인하는 방법을 학습합니다. **includes() 메서드** 배열에 특정 요소가 포함되어 있으면 true, 없으면 false를 반환합니다. **사용 예시** - [1, 2, 3].includes(2) → true - [1, 2, 3].includes(5) → false **indexOf와의 비교** - includes: 불리언 반환 (존재 여부만 확인) - indexOf: 인덱스 반환 (위치도 알 수 있음, 없으면 -1) **NaN 처리** includes는 NaN도 찾을 수 있지만 indexOf는 못 찾습니다: - [NaN].includes(NaN) → true - [NaN].indexOf(NaN) → -1
Solution Code
solution.js
function includes(arr, value) {
return arr.includes(value);
}Key Concepts from This Problem
1. includes 메서드
2. 배열 검색
3. 불리언 반환
4. NaN 처리
Common Mistakes
✗ 객체는 참조로 비교되므로 내용이 같아도 다른 객체는 찾지 못합니다
✗ 대소문자를 구분합니다
✗ 두 번째 인자로 검색 시작 인덱스를 지정할 수 있습니다
Hints
Hint 1: includes 메서드를 사용하세요.
Complexity Analysis
Time Complexity
O(n)
Grows linearly with input size
Space Complexity
O(1)
Uses almost no additional memory
Related Tags
#배열 #includes #검색