728x90
This file contains 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
// Filter anagrams | |
// importance: 4 | |
// Anagrams are words that have the same number of same letters, but in different order. | |
function aclean(arr) { | |
const map = new Map(); | |
for(word of arr) { | |
const sorted = word.toLowerCase().split('').sort().join(''); | |
map.set(sorted, word); | |
} | |
return Array.from(map.values()).join(', '); | |
} | |
let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"]; | |
console.log( aclean(arr) ); // "nap,teachers,ear" or "PAN,cheaters,era" | |
// 2 | |
// function aclean(arr) { | |
// const obj = {}; | |
// for(let i = 0; i < arr.length; i++) { | |
// const sorted = arr[i].toLowerCase().split('').sort().join(''); | |
// obj[sorted] = arr[i]; | |
// } | |
// return Object.values(obj).join(', '); | |
// } | |
// let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"]; | |
// console.log( aclean(arr) ); // "nap,teachers,ear" or "PAN,cheaters,era" |
'Algorithm > JavaScript(Node.js)' 카테고리의 다른 글
The Modern JS Tutorial: WeakMap and WeakSet: Store "unread" flags (0) | 2021.10.20 |
---|---|
The Modern JS Tutorial: Map and Set: iterable keys (0) | 2021.10.06 |
The Modern JS Tutorial: Map and Set: filter unique array members (0) | 2021.10.06 |
The Modern JS Tutorial: array-methods: Create keyed object from array (0) | 2021.09.23 |
The Modern JS Tutorial: array-methods: filter unique array members (0) | 2021.09.22 |