Back to all solutions

#389 - Find the Difference

Problem Description

You are given two strings s and t.

String t is generated by random shuffling string s and then add one more letter at a random position.

Return the letter that was added to t.

Solution

/**
 * @param {string} s
 * @param {string} t
 * @return {character}
 */
var findTheDifference = function(s, t) {
  const map = new Map();
  t.split('').forEach(c => map.set(c, (map.get(c) ?? 0) + 1));
  s.split('').forEach(c => map.set(c, map.get(c) - 1));
  return Array.from(map).find(([letter, count]) => count)[0];
};