📚
Find Index
Find Index쉬움 배열 +10pts
Problem
Write a function that returns the index of a value in an array, or -1 if not found.
Examples
Input:
findIndex([1, 2, 3], 2)Output:
1Input:
findIndex([1, 2, 3], 5)Output:
-1Explanation
이 문제는 **indexOf 메서드**를 사용하여 배열에서 요소의 위치를 찾는 방법을 학습합니다. **indexOf() 메서드** 배열에서 특정 요소의 첫 번째 인덱스를 반환합니다. 없으면 -1을 반환합니다. **사용 예시** - [1, 2, 3].indexOf(2) → 1 - [1, 2, 3].indexOf(5) → -1 **중복 요소가 있는 경우** 첫 번째로 발견된 인덱스만 반환합니다: - [1, 2, 2, 3].indexOf(2) → 1 **-1 반환의 의미** -1은 요소가 없음을 나타냅니다. 조건문에서 활용: ``...
View detailed explanation →Key Concepts
indexOf 메서드 인덱스 반환 -1 반환 의미 선형 검색
Time: O(n) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.