Skip to content

Instantly share code, notes, and snippets.

@ChristianOConnor
Created March 9, 2023 21:36
Show Gist options
  • Save ChristianOConnor/255c2738d4be4b7eb1c6573d95b7c397 to your computer and use it in GitHub Desktop.
Save ChristianOConnor/255c2738d4be4b7eb1c6573d95b7c397 to your computer and use it in GitHub Desktop.
anagram-finder-express-js
var express = require('express');
var router = express.Router();
router.post('/', function findAnagramsInArray(req, res) {
var words = JSON.parse(JSON.stringify(req.body));
let result = {}
for (const word of words) {
const sorted = word.split("").sort().join("");
if (sorted in result) {
result[sorted].push(word);
} else {
result[sorted] = [word];
}
}
res.send(result);
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment