πŸ“š

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: 1
Input: findIndex([1, 2, 3], 5)
Output: -1

Explanation

이 λ¬Έμ œλŠ” **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.