본문 바로가기
코테연습

22.Array.diff

by hxunz 2022. 3. 31.

Description:

Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.

It should remove all values from list a, which are present in list b keeping their order.

arrayDiff([1,2],[1]) == [2]

If a value is present in b, all of its occurrences must be removed from the other:

arrayDiff([1,2,2,2,3],[2]) == [1,3]

 

 

function arrayDiff(a, b) {
  return a.filter(x => !b.includes(x));
}

 

우선 , a 배열에 filter()를 사용했는데 

a 배열 내 요소중에 b 배열에서도 그 요소를 포함하지 않는다면 리턴하도록 해주었다.

 

'코테연습' 카테고리의 다른 글

24.Bit Counting  (0) 2022.04.01
23.Who likes it?  (0) 2022.04.01
21.Sum of Digits / Digital Root  (0) 2022.03.31
20.Stop gninnipS My sdroW!  (0) 2022.03.30
19.Find the odd int  (0) 2022.03.29

댓글