Created
March 9, 2023 21:36
-
-
Save ChristianOConnor/255c2738d4be4b7eb1c6573d95b7c397 to your computer and use it in GitHub Desktop.
anagram-finder-express-js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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