Skip to content

Instantly share code, notes, and snippets.

@rizkyputri13
Created March 3, 2023 05:01
Show Gist options
  • Save rizkyputri13/8a20099d85f10f46c17fc21329e8f1a9 to your computer and use it in GitHub Desktop.
Save rizkyputri13/8a20099d85f10f46c17fc21329e8f1a9 to your computer and use it in GitHub Desktop.
No. 1
// Terdapat sebuah array of strings sebagai berikut:
// ['cook', 'save', 'taste', 'aves', 'vase', 'state', 'map']
// Buatlah sebuah fungsi yang mengelompokkan sebuah array of strings di atas menjadi kumpulan anagram, dengan expected result sebagai berikut:
// [
// [ 'cook' ],
// [ 'save', 'aves', 'vase' ],
// [ 'taste', 'state' ],
// [ 'map' ]
// ]
function groupAnagrams(words) {
let anagram = {};
for (let word of words) {
let sortedWord = word.split("").sort().join("");
if (!anagram[sortedWord]) {
anagram[sortedWord] = [word];
} else {
anagram[sortedWord].push(word);
}
}
return Object.values(anagram);
}
let words = ["cook", "save", "taste", "aves", "vase", "state", "map"];
console.log(groupAnagrams(words));
No. 2
//Buatlah query SQL :
SELECT t1.id, t1.name, t2.name AS parent_name
FROM EMPLOYEE t1
LEFT JOIN EMPLOYEE t2 ON t1.parent_id = t2.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment