August 08, 2017

Javascript Hash Table

const findTheDifference = (s, t) => {
	let hash = {};
	for (let v of [...t]) {
		hash[v] ? hash[v]++ : (hash[v] = 1);
	}
	for (let v of [...s]) {
		hash[v]--;
	}
	for (let key in hash) {
		if (hash[key] === 1) return key;
	}
};

Ref: https://leetcode.com/problems/find-the-difference/description/