> For the complete documentation index, see [llms.txt](https://algorithm.prettylog.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/group-anagrams.md).

# Group Anagrams

![](/files/PrIu8GvpKwUzYWlZ2kpV)

* w \* nlogn, w \* n

```tsx
function groupAnagrams(words) {

  const map = {};
  
  for (let i = 0; i < words.length; i++) {
    const word = words[i];
    const sorted = [...word].sort().join('');

    if (map[sorted] === undefined)
      map[sorted] = [];

    map[sorted].push(word);
  }

  return Object.values(map);
  
}

// Do not edit the line below.
exports.groupAnagrams = groupAnagrams;
```
