Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.9 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
68af0687ef34c76c28ffa549 Challenge 31: Array Diff 28 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"].

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

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

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

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

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"].

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"].

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

--seed--

--seed-contents--

function arrayDiff(arr1, arr2) {

  return arr1;
}

--solutions--

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();
}