-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Labels
Utility FunctionUtility Function ImplementationUtility Function Implementation
Description
๋ฌธ์ ์ค๋ช | Array Difference Implementation
๋ฐฐ์ด์์ ๋ค๋ฅธ ๋ฐฐ์ด์ ์์๋ค์ ์ ์ธํ ์๋ก์ด ๋ฐฐ์ด์ ๋ง๋๋ ํจ์๋ฅผ ๊ตฌํ
๐ ์ ์ฝ์กฐ๊ฑด
array: ๊ฒ์ฌํ ๋ฐฐ์ดvalues: ์ ์ธํ ๊ฐ๋ค์ ๋ฐฐ์ด- SameValueZero ๋๋ฑ์ฑ ๋น๊ต ์ฌ์ฉ
- ๊ฒฐ๊ณผ ๊ฐ์ ์์๋ ์ฒซ ๋ฒ์งธ ๋ฐฐ์ด์ ๊ธฐ์ค์ผ๋ก ๊ฒฐ์ ๋จ
๐ก ์์
- Input:
difference([1, 2, 3], [2, 3])- Output:
[1]
- Output:
- Input:
difference([1, 2, 3], [])- Output:
[1, 2, 3]
- Output:
๋ฌธ์ ํด๊ฒฐ ๊ณผ์
Step 1: ๋ฌธ์ ์ดํดํ๊ธฐ
- ์์ ์์๋ก ์ง์ ์คํํด๋ณด๊ธฐ
[1, 2, 3], [2, 3] -> [1] // 2, 3์ด ์ ๊ฑฐ๋จ [1, , 3], [1] -> [3] // ๋น ์์๋ ๋ฌด์๋๊ณ 1์ด ์ ๊ฑฐ๋จ
Step 2: ์ ๊ทผ ๋ฐฉ๋ฒ
-
์ง๊ด์ ์ผ๋ก ์๊ฐํ๊ธฐ
- ์ฒซ ๋ฒ์งธ ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ์ํ
- ๊ฐ ์์๊ฐ ๋ ๋ฒ์งธ ๋ฐฐ์ด์ ์๋์ง ํ์ธ
- ์๋ ์์๋ง ๊ฒฐ๊ณผ ๋ฐฐ์ด์ ํฌํจ
-
์๋ฃ๊ตฌ์กฐ์ ์๊ณ ๋ฆฌ์ฆ ํ์ฉ
- Array.filter() ๋ฉ์๋ ์ฌ์ฉ
- Array.includes() ๋ฉ์๋๋ก ํฌํจ ์ฌ๋ถ ํ์ธ
Step 3: ์ฝ๋ ์ค๊ณ
- array.filter() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์๋ก์ด ๋ฐฐ์ด ์์ฑ
- ๊ฐ ์์๋ง๋ค:
- values ๋ฐฐ์ด์ ํด๋น ์์๊ฐ ์๋์ง ํ์ธ
- ์์ผ๋ฉด true๋ฅผ ๋ฐํํ์ฌ ๊ฒฐ๊ณผ ๋ฐฐ์ด์ ํฌํจ
- ์์ผ๋ฉด false๋ฅผ ๋ฐํํ์ฌ ๊ฒฐ๊ณผ ๋ฐฐ์ด์์ ์ ์ธ
Step 4: ์ฝ๋ ๊ตฌํ
/**
* @param {Array} array - Array from which different elements are to be removed.
* @param {Array} values - Array of values that are to be removed from the original array.
* @return {Array} Returns filtered array.
*/
export default function difference(array, values) {
return array.filter((item) => {
return !values.includes(item)
})
}Metadata
Metadata
Assignees
Labels
Utility FunctionUtility Function ImplementationUtility Function Implementation