1
+ /*
2
+ Diff Two Arrays:
3
+ Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both.
4
+ In other words, return the symmetric difference of the two arrays.
5
+
6
+ Note: You can return the array with its elements in any order.
7
+
8
+ - diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]) should return an array.
9
+ - ["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return ["pink wool"].
10
+ - ["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return an array with one item.
11
+ - ["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return ["diorite", "pink wool"].
12
+ - ["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"] should return an array with two items.
13
+ - ["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"] should return [].
14
+ - ["andesite", "grass", "dirt", "dead shrub"], ["andesite", "grass", "dirt", "dead shrub"] should return an empty array.
15
+ - [1, 2, 3, 5], [1, 2, 3, 4, 5] should return [4].
16
+ - [1, 2, 3, 5], [1, 2, 3, 4, 5] should return an array with one item.
17
+ - [1, "calf", 3, "piglet"], [1, "calf", 3, 4] should return ["piglet", 4].
18
+ - [1, "calf", 3, "piglet"], [1, "calf", 3, 4] should return an array with two items.
19
+ - [], ["snuffleupagus", "cookie monster", "elmo"] should return ["snuffleupagus", "cookie monster", "elmo"].
20
+ - [], ["snuffleupagus", "cookie monster", "elmo"] should return an array with three items.
21
+ - [1, "calf", 3, "piglet"], [7, "filly"] should return [1, "calf", 3, "piglet", 7, "filly"].
22
+ - [1, "calf", 3, "piglet"], [7, "filly"] should return an array with six items.
23
+ */
24
+ function diffArray ( arr1 , arr2 ) {
25
+ const newArr = [ ] ;
26
+ arr1
27
+ . filter ( value => ! arr2 . includes ( value ) )
28
+ . map ( value => newArr . push ( value ) ) ;
29
+ arr2
30
+ . filter ( value => ! arr1 . includes ( value ) )
31
+ . map ( value => newArr . push ( value ) ) ;
32
+ return newArr ;
33
+ }
34
+
35
+ console . log ( "expects: [4], returns: " , diffArray ( [ 1 , 2 , 3 , 5 ] , [ 1 , 2 , 3 , 4 , 5 ] ) ) ;
36
+ console . log ( "expects: [\"pink wool\"], returns: " , diffArray ( [ "diorite" , "andesite" , "grass" , "dirt" , "pink wool" , "dead shrub" ] , [ "diorite" , "andesite" , "grass" , "dirt" , "dead shrub" ] ) ) ;
37
+ console . log ( "expects: [\"diorite\", \"pink wool\"], returns: " , diffArray ( [ "andesite" , "grass" , "dirt" , "pink wool" , "dead shrub" ] , [ "diorite" , "andesite" , "grass" , "dirt" , "dead shrub" ] ) ) ;
38
+ console . log ( "expects: [], returns: " , diffArray ( [ "andesite" , "grass" , "dirt" , "dead shrub" ] , [ "andesite" , "grass" , "dirt" , "dead shrub" ] ) ) ;
39
+ console . log ( "expects: [\"piglet\", 4], returns: " , diffArray ( [ 1 , "calf" , 3 , "piglet" ] , [ 1 , "calf" , 3 , 4 ] ) ) ;
40
+ console . log ( "expects: [\"snuffleupagus\", \"cookie monster\", \"elmo\"], returns: " , diffArray ( [ ] , [ "snuffleupagus" , "cookie monster" , "elmo" ] ) ) ;
41
+ console . log ( "expects: [1, \"calf\", 3, \"piglet\", 7, \"filly\"], returns: " , diffArray ( [ 1 , "calf" , 3 , "piglet" ] , [ 7 , "filly" ] ) ) ;
0 commit comments