题目![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6156329d43c13face47545090a7c5e1b.png)
代码
var findTheDifference = function(s, t) {
const map = new Map()
for (let i = 0; i < s.length; i++) {
if (map.has(s[i])) {
map.set(s[i], map.get(s[i]) + 1)
} else {
map.set(s[i], 1)
}
}
for (let j = 0; j < t.length; j++) {
if (map.has(t[j])) {
if (map.get(t[j]) - 1 < 0) {
return t[j]
} else {
map.set(t[j], map.get(t[j]) - 1)
}
} else {
return t[j]
}
}
};
var findTheDifference = function(s, t) {
const arr = new Array(26).fill(0)
s.split('').forEach(item => {
arr[item.charCodeAt() - 'a'.charCodeAt()]++
})
let res = ''
t.split('').forEach(item => {
arr[item.charCodeAt() - 'a'.charCodeAt()]--
if (arr[item.charCodeAt() - 'a'.charCodeAt()] < 0) {
res = item
}
})
return res
}