티스토리 뷰

  • 문제

    Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both.

    • 예시
    diffArray(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]) // should return ["diorite", "pink wool"].
    

     

  • 내가 푼 답

    function diffArray(arr1, arr2) {
      return [...arr1.filter(n => arr2.indexOf(n) === -1), ...arr2.filter(n => arr1.indexOf(n) === -1)]; 
    }
    

     

  • Advanced Solution

    function diffArray(arr1, arr2) {
        return arr1
          .filter(el => !arr2.includes(el))
          .concat(
            arr2.filter(el => !arr1.includes(el))
          )
    }
    

     

 

 

'Data Structure & Algorithm' 카테고리의 다른 글

분할정복  (0) 2019.03.28
JS_Pig Latin  (0) 2019.03.21
JS_Wherefore Art Thou  (0) 2019.03.21
JS_Sum All Numbers in Range  (0) 2019.03.20
JS_Seek and Destroy  (0) 2019.03.13
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함