π
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.