JSFiddle - React, Tailwind, and code Playground

by Abdul Ahmad

JavaScript

// - given an array of anagrams, output each set of anagrams

const anagrams = [
	'cat', 'cta', 'act', 'act',
  'box', 'xob',
  'toy', 'yot', 'oty',
];

function analyzeAnagrams() {
	const result = anagrams.reduce((acc, i) => {
  	const sorted = i.split('').sort().join();
    const set = acc[sorted] || [];
    
    set.push(i);
    acc[sorted] = set;
    
    return acc;
  }, {});
  console.log('result: ', result);
}

analyzeAnagrams();