Here is the original problem, and here's my solution:
function main() {
var m_temp = readLine().split(' ');
var m = parseInt(m_temp[0]);
var n = parseInt(m_temp[1]);
magazine = readLine().split(' ');
ransom = readLine().split(' ');
var freqs = {}
for (var i = 0; i < m; i++){
freqs[magazine[i]] = (freqs[magazine[i]] || 0) + 1;
}
var result = "Yes"
for (var j = 0; j < n; j++){
if (freqs[ransom[j]] && freqs[ransom[j]] > 0){
freqs[ransom[j]] -= 1;
} else {
result = "No"
break;
}
}
console.log(result)
}
I wonder if there's a more efficient solution than this? Thanks! I understand forEach
could be used for code brevity, but I'm just using for loop for the extra performance benefit (https://coderwall.com/p/kvzbpa/don-t-use-array-foreach-use-for-instead)
if (!--freqs[ransom[i]] >= 0){result = "No"; break;}
\$\endgroup\$