DEV Community

Madalina Pastiu
Madalina Pastiu

Posted on

Array.diff

Instructions:

Implement a function that computes the difference between two lists. The function should remove all occurrences of elements from the first list (a) that are present in the second list (b). The order of elements in the first list should be preserved in the result.

Examples
If a = [1, 2] and b = [1], the result should be [2].

If a = [1, 2, 2, 2, 3] and b = [2], the result should be [1, 3].

Thoughts:

1.I use the filter() method on array a and each element of it checks if is not included in array b. The element transfer to the newList only if array b doesn't contain it.

Solution:

function arrayDiff(a, b) {
  const newList= a.filter(el => !b.includes(el));
return newList;
    }
Enter fullscreen mode Exit fullscreen mode

This is a CodeWars Challenge of 6kyu Rank

Top comments (0)