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
2021-01-13 03:31:00 +01:00
dashedName: symmetric-difference
2018-12-30 19:05:56 +05:30
---
2020-11-27 19:02:05 +01:00
# --description--
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
Given two [set ](https://rosettacode.org/wiki/set )s *A* and *B* , compute $(A \\setminus B) \\cup (B \\setminus A).$ That is, enumerate the items that are in *A* or *B* but not both. This set is called the [symmetric difference ](<https://en.wikipedia.org/wiki/Symmetric difference> ) of *A* and *B* . In other words: $(A \\cup B) \\setminus (A \\cap B)$ (the set of items that are in at least one of *A* or *B* minus the set of items that are in both *A* and *B* ).
2019-07-18 17:32:12 +02:00
2020-11-27 19:02:05 +01:00
# --instructions--
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
Write a function that takes two arrays as parameters and returns the symmetric difference. Sort the resultant array before returning it.
2018-12-30 19:05:56 +05:30
2020-11-27 19:02:05 +01:00
# --hints--
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
`symmetricDifference` should be a function.
2019-07-18 17:32:12 +02:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof symmetricDifference == 'function');
```
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
`symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])` should return an array.
```js
assert(
Array.isArray(
symmetricDifference(
['John', 'Bob', 'Mary', 'Serena'],
['Jim', 'Mary', 'John', 'Bob']
)
)
);
2018-12-30 19:05:56 +05:30
```
2020-11-27 19:02:05 +01:00
`symmetricDifference(["John", "Bob", "Mary", "Serena"], ["Jim", "Mary", "John", "Bob"])` should return `["Jim", "Serena"]` .
```js
assert.deepEqual(
symmetricDifference(
['John', 'Bob', 'Mary', 'Serena'],
['Jim', 'Mary', 'John', 'Bob']
),
['Jim', 'Serena']
);
```
2018-12-30 19:05:56 +05:30
2020-11-27 19:02:05 +01:00
`symmetricDifference([1, 2, 3], [3, 4])` should return `[1, 2, 4]` .
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(symmetricDifference([1, 2, 3], [3, 4]), [1, 2, 4]);
```
2019-07-18 17:32:12 +02:00
2020-11-27 19:02:05 +01:00
`symmetricDifference([1, 2, 3, 4, 5], [3, 4, 8, 7])` should return `[1, 2, 5, 7, 8]` .
2018-12-30 19:05:56 +05:30
```js
2020-11-27 19:02:05 +01:00
assert.deepEqual(symmetricDifference([1, 2, 3, 4, 5], [3, 4, 8, 7]), [
1,
2,
5,
7,
8
]);
```
2020-09-15 09:57:40 -07:00
2020-11-27 19:02:05 +01:00
`symmetricDifference([1, 2, 3, 4, 5, 6, 7, 8], [1, 3, 5, 6, 7, 8, 9])` should return `[2, 4, 9]` .
```js
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
```
2020-11-27 19:02:05 +01:00
`symmetricDifference([1, 2, 4, 7, 9], [2, 3, 7, 8, 9])` should return `[1, 3, 4, 8]` .
2018-12-30 19:05:56 +05:30
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(symmetricDifference([1, 2, 4, 7, 9], [2, 3, 7, 8, 9]), [
1,
3,
4,
8
]);
```
2020-03-30 11:23:18 -05:00
2020-11-27 19:02:05 +01:00
# --seed--
## --seed-contents--
```js
function symmetricDifference(A, B) {
}
```
# --solutions--
2018-12-30 19:05:56 +05:30
```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
}
```