πŸ“š

Array Difference

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

Problem

Write a function that returns elements in the first array but not in the second.

Examples

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

Explanation

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

View detailed explanation β†’

Key Concepts

filter λ©”μ„œλ“œ λΆ€μ • μ—°μ‚°μž μ°¨μ§‘ν•© μ§‘ν•© μ—°μ‚°
Time: O(n*m) Space: O(n)
solution.js
Ctrl + Enter
Run tests to see results here.