0

I'm new to fairly new to Javascript and I need some help solving the 804. Unique Morse Code Words - Leetcode problem.

I figured how to search return the morse code by using the index of each letter from a word and using it to concatenate the codes at those specific index in the morse code array. The problem is I can't store the results into an Set array excluding the duplicates and returning the length of the Set array.

var letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
var morseCode = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."];
var words = ["gin", "zen", "gig", "msg"]
var uniqueMorseRepresentations = function(words) {
  for (i = 0; i < words.length; i++) {
    let word = words[i];
    var result = "";
    for (j = 0; j < word.length; j++) {
      let letter = word.charAt(j);
      let index = letters.indexOf(letter);
      result += morseCode[index];
    };
    console.log(result);
  };
};
uniqueMorseRepresentations(words);

The console.log method return the results in 4 separate strings but I don't know how to store them into an array while verifying if there are duplicate.

I'm sorry if the question is sloppy. It's my first one.

Thanks in advance!

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    Why can't you store the results into a `Set`? Where is your code that tries to do that? – Barmar Aug 15 '19 at 13:00
  • You could create an object containing each string, by starting with results = {}, then populate with results["Your string"] = true; This would only allow you to create one entry for each string, then at the end you can iterate through the object with for( let str : results ) {} – SPlatten Aug 15 '19 at 13:02
  • Possible duplicate of [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – VLAZ Aug 15 '19 at 13:03

2 Answers2

1

Inside your function, create a Set:

 const resultSet = new Set();

Then when each result is built up (when you log it), add the resulting morse code to that Set:

resultSet.add(result);

Then you can finally return that Set, or its .size.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

I think this should solve your problem. Take an obj, and store the result in that and check if the result is repeating then don't push that result into that array. And the time complexity in this operation would be O(1), so you don't have to worry about it if you want to scale your algorithm.

var letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
var morseCode = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."];
var words = ["gin", "zen", "gig", "msg","gig"]
var array  = [];
var uniqueMorseRepresentations = function(words) {
  let obj = {};
  for (i = 0; i < words.length; i++) {
    let word = words[i];
    var result = "";
    for (j = 0; j < word.length; j++) {
      let letter = word.charAt(j);
      let index = letters.indexOf(letter);
      result += morseCode[index];
    };
    if(!obj[result]){
        obj[result] = result;
        array.push(result);
    }
    console.log(result);
  };
    console.log(array)
};
uniqueMorseRepresentations(words);