πŸ“š

Array Intersection

Array Intersection
보톡 λ°°μ—΄ +20pts

Problem

Write a function that returns elements common to both arrays.

Examples

Input: intersection([1, 2, 3], [2, 3, 4])
Output: [2, 3]

Explanation

이 λ¬Έμ œλŠ” **filter와 includes**λ₯Ό μ‘°ν•©ν•˜μ—¬ 두 λ°°μ—΄μ˜ ꡐ집합을 κ΅¬ν•˜λŠ” 방법을 ν•™μŠ΅ν•©λ‹ˆλ‹€. ## 핡심 κ°œλ…: μ§‘ν•© μ—°μ‚° - ꡐ집합 ꡐ집합은 두 집합에 κ³΅ν†΅μœΌλ‘œ μ‘΄μž¬ν•˜λŠ” μš”μ†Œλ“€μ˜ μ§‘ν•©μž…λ‹ˆλ‹€. ### μ ‘κ·Ό 방법 ```javascript arr1.filter(x => arr2.includes(x)) ``` - arr1의 각 μš”μ†Œμ— λŒ€ν•΄ - arr2에 ν¬ν•¨λ˜μ–΄ μžˆλŠ”μ§€ 확인 - ν¬ν•¨λœ μš”μ†Œλ§Œ 남김 ### filter와 includes μ‘°ν•© 이 νŒ¨ν„΄μ€ μ§‘ν•© μ—°μ‚°μ—μ„œ 맀우 자주 μ‚¬μš©λ©λ‹ˆλ‹€: - ꡐ집합: filter(x =>...

View detailed explanation β†’

Key Concepts

filter λ©”μ„œλ“œ includes λ©”μ„œλ“œ ꡐ집합 μ§‘ν•© μ—°μ‚°
Time: O(n*m) Space: O(min(n,m))
solution.js
Ctrl + Enter
Run tests to see results here.