JavaScript: Find the difference of two arrays
JavaScript Array: Exercise-23 with Solution
Difference Between Arrays
Write a JavaScript function to find the difference between two arrays.
Test Data:
console.log(difference([1, 2, 3], [100, 2, 1, 10]));
["3", "10", "100"]
console.log(difference([1, 2, 3, 4, 5], [1, [2], [3, [[4]]],[5,6]]));
["6"]
console.log(difference([1, 2, 3], [100, 2, 1, 10]));
["3", "10", "100"]
Visual Presentation:

Sample Solution:
JavaScript Code:
Output:
[3,10,100] [6]
Flowchart:

ES6 Version:
Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript function that finds the difference between two arrays, returning elements present in one but not in the other.
- Write a JavaScript function that computes the symmetric difference between two arrays using nested filters.
- Write a JavaScript function that compares two arrays and returns the items unique to each array.
- Write a JavaScript function that performs a deep difference on arrays containing nested arrays.
Improve this sample solution and post your code through Disqus.
Previous: Write a JavaScript program to compute the union of two arrays.
Next: Write a JavaScript function to remove. 'null', '0', '""', 'false', 'undefined' and 'NaN' values from an array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.