You are viewing a single comment's thread. Return to all comments →
function beautifulPairs(A, B) { let mp = {}; let result = 0; for (let i = 0; i < A.length; i++) { mp[A[i]] = (mp[A[i]] || 0) + 1; } for (let i = 0; i < B.length; i++) { if (mp[B[i]] > 0) { mp[B[i]]--; result++; } } const isEmpty = Object.keys(mp).every(key => mp[key] === 0); return result === A.length ? (isEmpty ? result : result - 1) : result + 1; } console.log(beautifulPairs([1, 2, 3, 4], [1, 2, 4, 3]));
Seems like cookies are disabled on this browser, please enable them to open this website
Beautiful Pairs
You are viewing a single comment's thread. Return to all comments →