-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDiff.js
More file actions
43 lines (24 loc) · 1.09 KB
/
ArrayDiff.js
File metadata and controls
43 lines (24 loc) · 1.09 KB
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
32
33
34
35
36
37
38
39
40
41
42
//6 Kyu
//Array.diff
//Fundamentals, Arrays, Algoruthms
// 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]
//Solution I
function arrayDiff(a, b) {
//we will need a loop to iteratate and check both arrays
//we use filter to create a new array and pass the unique elements IF and ONLY IF the element in b is not present.
//the ! operator ensures the filter function gives us a flasey return when the same element is present in b.
let newArr = a.filter((element)=> !b.includes(element))
//return the new array
return newArr
}
//Parameters
//take two arrays, if val from one is in two remove, if val from two is in one remove.
//returns
//a single new array with filtered values from both
//examples
// arrayDiff([1,2],[1]) == [2]
// arrayDiff([1,2,2,2,3],[2]) == [1,3]