Exercise 20: Find Array Symmetric Difference
Problem Statement
Create a function `symmetricDifference(arr1, arr2)` that returns elements not common in both arrays.
Example: symmetricDifference([1, 2, 3], [2, 3, 4]) → [1, 4]
Sample Output:
symmetricDifference([1, 2, 3], [2, 3, 4]) => [1, 4] symmetricDifference([1, 2], [3, 4]) => [1, 2, 3, 4]
Solution
const symmetricDifference = (arr1, arr2) => {
const set1 = new Set(arr1), set2 = new Set(arr2);
return [...arr1.filter(x => !set2.has(x)), ...arr2.filter(x => !set1.has(x))];
};Explanation
Overall Goal:
- Dono arrays me symmetric difference find karna.
Real world:
- Data comparison: unique items find.
- Set operations: symmetric difference.
Navigation
Previous
Exercise 19
Next
No next exercise