Files
freecodecamp--freecodecamp/curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69cfca90e8a0a6d4d6871c4e.md
T
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

2.8 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69cfca90e8a0a6d4d6871c4e Challenge 264: Anagram Groups 28 challenge-264

--description--

Given an array of words, return a 2d array of the words grouped into anagrams.

  • Words are anagrams if they contain the same letters in any order.
  • Each word belongs to exactly one group.
  • Return order doesn't matter.

For example, given ["listen", "silent", "hello", "enlist", "world"], return [["listen", "silent", "enlist"], ["hello"], ["world"]].

--hints--

groupAnagrams(["listen", "silent", "hello", "enlist", "world"]) should return [["listen", "silent", "enlist"], ["hello"], ["world"]].

const groups = groupAnagrams(["listen", "silent", "hello", "enlist", "world"]);
const sorted = groups.map(g => g.sort()).sort((a, b) => a[0].localeCompare(b[0]));
assert.deepEqual(sorted, [["enlist", "listen", "silent"], ["hello"], ["world"]]);

groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]) should return [["ate", "eat", "tea"], ["bat"], ["nat", "tan"]].

const groups = groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]);
const sorted = groups.map(g => g.sort()).sort((a, b) => a[0].localeCompare(b[0]));
assert.deepEqual(sorted, [["ate", "eat", "tea"], ["bat"], ["nat", "tan"]]);

groupAnagrams(["care", "race", "acre", "pots", "stop", "tops", "opts", "post", "spot", "evil", "vile", "live", "veil"]) should return [["acre", "care", "race"], ["evil", "live", "veil", "vile"], ["opts", "post", "pots", "spot", "stop", "tops"]].

const groups = groupAnagrams(["care", "race", "acre", "pots", "stop", "tops", "opts", "post", "spot", "evil", "vile", "live", "veil"]);
const sorted = groups.map(g => g.sort()).sort((a, b) => a[0].localeCompare(b[0]));
assert.deepEqual(sorted, [["acre", "care", "race"], ["evil", "live", "veil", "vile"], ["opts", "post", "pots", "spot", "stop", "tops"]]);

groupAnagrams(["algorithms", "logarithms", "education", "cautioned", "auctioned", "triangle", "integral", "alerting", "relating"]) should return [["alerting", "integral", "relating", "triangle"], ["algorithms", "logarithms"], ["auctioned", "cautioned", "education"]].

const groups = groupAnagrams(["algorithms", "logarithms", "education", "cautioned", "auctioned", "triangle", "integral", "alerting", "relating"]);
const sorted = groups.map(g => g.sort()).sort((a, b) => a[0].localeCompare(b[0]));
assert.deepEqual(sorted, [["alerting", "integral", "relating", "triangle"], ["algorithms", "logarithms"], ["auctioned", "cautioned", "education"]]);

--seed--

--seed-contents--

function groupAnagrams(words) {

  return words;
}

--solutions--

function groupAnagrams(words) {
  const map = {};
  for (const word of words) {
    const key = word.split('').sort().join('');
    if (!map[key]) map[key] = [];
    map[key].push(word);
  }
  return Object.values(map);
}