2018-12-30 19:05:56 +05:30
---
id: 5a23c84252665b21eecc8046
title: Symmetric difference
challengeType: 5
2019-07-31 11:32:23 -07:00
forumTopicId: 16086
2018-12-30 19:05:56 +05:30
---
## Description
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< section id = 'description' >
2019-07-18 17:32:12 +02:00
2019-05-22 23:30:29 +09:00
Given two < a href = "https://rosettacode.org/wiki/set" target = "_blank" > set</ a > s < i > A</ i > and < i > B</ i > , compute $(A \setminus B) \cup (B \setminus A).$
2019-03-10 19:14:48 +09:00
That is, enumerate the items that are in < i > A< / i > or < i > B< / i > but not both. This set is called the < a href = "https://en.wikipedia.org/wiki/Symmetric difference" target = "_blank" > symmetric difference< / a > of < i > A< / i > and < i > B< / i > .
2018-12-30 19:05:56 +05:30
In other words: $(A \cup B) \setminus (A \cap B)$ (the set of items that are in at least one of < i > A</ i > or < i > B</ i > minus the set of items that are in both < i > A</ i > and < i > B</ i > ).
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< / section >
## Instructions
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< section id = 'instructions' >
2019-07-18 17:32:12 +02:00
2019-03-10 19:14:48 +09:00
Write a function that takes two arrays as parameters and returns the symmetric difference. Sort the resultant array before returning it.
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< / section >
## Tests
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< section id = 'tests' >
2020-03-30 11:23:18 -05:00
```yml
2018-12-30 19:05:56 +05:30
tests:
- text: < code > symmetricDifference</ code > should be a function.
2020-03-30 11:23:18 -05:00
testString: assert(typeof symmetricDifference == 'function');
2019-03-10 19:14:48 +09:00
- text: < code > symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])</ code > should return an array.
2020-03-30 11:23:18 -05:00
testString: assert(Array.isArray(symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])));
2018-12-30 19:05:56 +05:30
- text: < code > symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])</ code > should return < code > ["Jim", "Serena"]</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.deepEqual(symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"]), ["Jim", "Serena"]);
2018-12-30 19:05:56 +05:30
- text: < code > symmetricDifference([1, 2, 3], [3, 4])</ code > should return < code > [1, 2, 4]</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.deepEqual(symmetricDifference([1, 2, 3], [3, 4]), [1, 2, 4]);
2018-12-30 19:05:56 +05:30
- text: < code > symmetricDifference([1, 2, 3, 4, 5], [3, 4, 8, 7])</ code > should return < code > [1, 2, 5, 7, 8]</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.deepEqual(symmetricDifference([1, 2, 3, 4, 5], [3, 4, 8, 7]), [1, 2, 5, 7, 8]);
2018-12-30 19:05:56 +05:30
- text: < code > symmetricDifference([1, 2, 3, 4, 5, 6, 7, 8], [1, 3, 5, 6, 7, 8, 9])</ code > should return < code > [2, 4, 9]</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.deepEqual(symmetricDifference([1, 2, 3, 4, 5, 6, 7, 8], [1, 3, 5, 6, 7, 8, 9]), [2, 4, 9]);
2018-12-30 19:05:56 +05:30
- text: < code > symmetricDifference([1, 2, 4, 7, 9], [2, 3, 7, 8, 9])</ code > should return < code > [1, 3, 4, 8]</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.deepEqual(symmetricDifference([1, 2, 4, 7, 9], [2, 3, 7, 8, 9]), [1, 3, 4, 8]);
2018-12-30 19:05:56 +05:30
```
< / section >
## Challenge Seed
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< section id = 'challengeSeed' >
2019-07-18 17:32:12 +02:00
2018-12-30 19:05:56 +05:30
< div id = 'js-seed' >
```js
2019-03-10 19:14:48 +09:00
function symmetricDifference(A, B) {
2018-12-30 19:05:56 +05:30
// Good luck!
}
```
< / div >
< / section >
## Solution
2020-03-30 11:23:18 -05:00
2018-12-30 19:05:56 +05:30
< section id = 'solution' >
```js
2019-03-10 19:14:48 +09:00
function symmetricDifference(A, B) {
2018-12-30 19:05:56 +05:30
function relative_complement(A, B) {
return A.filter(function(elem) {
2020-03-30 11:23:18 -05:00
return B.indexOf(elem) == -1;
2018-12-30 19:05:56 +05:30
});
}
function unique(ary) {
var u = ary.concat().sort();
2020-03-30 11:23:18 -05:00
for (var i = 1; i < u.length ; ) {
if (u[i - 1] === u[i]) u.splice(i, 1);
else i++;
2018-12-30 19:05:56 +05:30
}
return u;
}
2020-03-30 11:23:18 -05:00
return unique(
relative_complement(A, B).concat(relative_complement(B, A))
).sort();
2018-12-30 19:05:56 +05:30
}
```
2019-07-18 17:32:12 +02:00
< / section >