๐Ÿ“š

Array Includes

Array Includes
์‰ฌ์›€ ๋ฐฐ์—ด +10pts

Problem

Write a function that checks if an array includes a specific value.

Examples

Input: includes([1, 2, 3], 2)
Output: true
Input: includes([1, 2, 3], 5)
Output: false

Explanation

์ด ๋ฌธ์ œ๋Š” **includes ๋ฉ”์„œ๋“œ**๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์— ํŠน์ • ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ํ•™์Šตํ•ฉ๋‹ˆ๋‹ค. **includes() ๋ฉ”์„œ๋“œ** ๋ฐฐ์—ด์— ํŠน์ • ์š”์†Œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด true, ์—†์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. **์‚ฌ์šฉ ์˜ˆ์‹œ** - [1, 2, 3].includes(2) โ†’ true - [1, 2, 3].includes(5) โ†’ false **indexOf์™€์˜ ๋น„๊ต** - includes: ๋ถˆ๋ฆฌ์–ธ ๋ฐ˜ํ™˜ (์กด์žฌ ์—ฌ๋ถ€๋งŒ ํ™•์ธ) - indexOf: ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜ (์œ„์น˜๋„ ์•Œ ์ˆ˜ ์žˆ์Œ, ์—†์œผ๋ฉด -1) **NaN ์ฒ˜๋ฆฌ*...

View detailed explanation โ†’

Key Concepts

includes ๋ฉ”์„œ๋“œ ๋ฐฐ์—ด ๊ฒ€์ƒ‰ ๋ถˆ๋ฆฌ์–ธ ๋ฐ˜ํ™˜ NaN ์ฒ˜๋ฆฌ
Time: O(n) Space: O(1)
solution.js
Ctrl + Enter
Run tests to see results here.