Created
April 16, 2024 13:17
-
-
Save yevmoroz/2b8b9612c4e01e7b60c163d0fe67b6d5 to your computer and use it in GitHub Desktop.
leetcode uncommon words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function uncommonWords(s1, s2) { | |
const words1 = s1.split(' '); | |
const words2 = s2.split(' '); | |
const freq = new Map(); | |
for (const word of words1) { | |
freq.set(word, (freq.get(word) || 0) + 1); | |
} | |
for (const word of words2) { | |
freq.set(word, (freq.get(word) || 0) + 1); | |
} | |
const result = []; | |
for (const [word, count] of freq) { | |
if (count === 1) { | |
result.push(word); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment