LOGO100 logo

Anagrams

You will receive an array of strings.

Submit that array of strings, sorted from A-Z, but only supply the words that are an anagram of another word in that array.

Example puzzle

[ "kiwi", "melon", "apple", "lemon" ]

Example solution

[ "lemon", "melon" ]

Actual data used in the competition

Explanation

This challenge required you to split words into characters and sort them:

Code solutions

There are many ways to solve this. This JavaScript solution is nice and short:

let sortobj = {};
let anagrams = [];

puzzle.forEach(function (item) { 
    let id = item.split('').sort().join('');
    if (!sortobj[id]) { sortobj[id] = []; }
    sortobj[id].push(item); 
});

for (let i in sortobj) {
    if (sortobj[i].length > 1) {
        sortobj[i].forEach(function (e) { 
            anagrams.push(e); 
        });
    }
}

anagrams.sort();

Other submissions:

Back to all puzzles