Find the Difference | LeetCode 389

AFFILIATE LINKS

Great resource I use to learn algorithms.
40% off Tech Interview Pro: http://techinterviewpro.com/terriblewhiteboard
20% off CoderPro: http://coderpro.com/terriblewhiteboard

Here is the full implementation.

/**
 * @param {string} s
 * @param {string} t
 * @return {character}
 */
let findTheDifference = function(s, t) {
  let frequencies = {};

  for (let char of s) {
    if (frequencies[char] === undefined) {
      frequencies[char] = 1;
    } else {
      frequencies[char]++;
    }
  }

  for (let char of t) {
    if (frequencies[char] !== undefined && frequencies[char] > 0) {
      frequencies[char]--;
    } else {
      return char;
    }
  }

  return -1;
};