fork download
  1. class Kamus {
  2. constructor() {
  3. this.data = new Map();
  4. }
  5.  
  6. tambah(kata, sinonimList) {
  7. if (!this.data.has(kata)) {
  8. this.data.set(kata, new Set());
  9. }
  10.  
  11. sinonimList.forEach(sinonim => {
  12. this.data.get(kata).add(sinonim);
  13.  
  14. if (!this.data.has(sinonim)) {
  15. this.data.set(sinonim, new Set());
  16. }
  17. this.data.get(sinonim).add(kata);
  18. });
  19. }
  20.  
  21. ambilSinonim(kata) {
  22. if (!this.data.has(kata)) return null;
  23.  
  24. return Array.from(this.data.get(kata));
  25. }
  26. }
  27.  
  28. const kamus = new Kamus();
  29. kamus.tambah('big', ['large', 'great']);
  30. kamus.tambah('big', ['huge', 'fat']);
  31. kamus.tambah('huge', ['enormous', 'gigantic']);
  32.  
  33. console.log(kamus.ambilSinonim('big'));
  34. console.log(kamus.ambilSinonim('huge'));
  35. console.log(kamus.ambilSinonim('gigantic'));
  36. console.log(kamus.ambilSinonim('colossal'));
Success #stdin #stdout 0.04s 17180KB
stdin
Standard input is empty
stdout
large,great,huge,fat
big,enormous,gigantic
huge
null