-
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