题目描述
分析
找字符频率,肯定要想到用哈希表喽
解题思路
统计每个单词的频率,找一次的
过程
声明哈希表实例 map
把两个 string 合成一个数组,遍历他
统计每个单词的频率
返回频率为 1 的
代码
/**
* @param {string} s1
* @param {string} s2
* @return {string[]}
*/
var uncommonFromSentences = function(s1, s2) {
const map = new Map()
const arr = (s1 + ' ' + s2).split(' ')
const ret = []
for (const x of arr) {
map.set(x, map.get(x) ? map.get(x) + 1 : 1)
}
for (const x of map.keys()) {
if (map.get(x) === 1) ret.push(x)
}
return ret
};