---
id: 68af0687ef34c76c28ffa549
title: "Challenge 31: Array Diff"
challengeType: 28
dashedName: challenge-31
---

# --description--

Given two arrays with strings values, return a new array containing all the values that appear in only one of the arrays.

- The returned array should be sorted in alphabetical order.

# --hints--

`arrayDiff(["apple", "banana"], ["apple", "banana", "cherry"])` should return `["cherry"]`.

```js
assert.deepEqual(arrayDiff(["apple", "banana"], ["apple", "banana", "cherry"]), ["cherry"]);
```

`arrayDiff(["apple", "banana", "cherry"], ["apple", "banana"])` should return `["cherry"]`.

```js
assert.deepEqual(arrayDiff(["apple", "banana", "cherry"], ["apple", "banana"]), ["cherry"]);
```

`arrayDiff(["one", "two", "three", "four", "six"], ["one", "three", "eight"])` should return `["eight", "four", "six", "two"]`.

```js
assert.deepEqual(arrayDiff(["one", "two", "three", "four", "six"], ["one", "three", "eight"]), ["eight", "four", "six", "two"]);
```

`arrayDiff(["two", "four", "five", "eight"], ["one", "two", "three", "four", "seven", "eight"])` should return `["five", "one", "seven", "three"]`.

```js
assert.deepEqual(arrayDiff(["two", "four", "five", "eight"], ["one", "two", "three", "four", "seven", "eight"]), ["five", "one", "seven", "three"]);
```

`arrayDiff(["I", "like", "freeCodeCamp"], ["I", "like", "rocks"])` should return `["freeCodeCamp", "rocks"]`.

```js
assert.deepEqual(arrayDiff(["I", "like", "freeCodeCamp"], ["I", "like", "rocks"]), ["freeCodeCamp", "rocks"]);
```

# --seed--

## --seed-contents--

```js
function arrayDiff(arr1, arr2) {

  return arr1;
}
```

# --solutions--

```js
function arrayDiff(arr1, arr2) {
  let unique1 = arr1.filter((v, i) => arr1.indexOf(v) === i);
  let unique2 = arr2.filter((v, i) => arr2.indexOf(v) === i);

  let only1 = unique1.filter(v => !unique2.includes(v));
  let only2 = unique2.filter(v => !unique1.includes(v));

  return [...only1, ...only2].sort();
}
```
