You are viewing a single comment's thread. Return to all comments →
Using a JS object provides a faster algorithm because it doesn't require the sort operation:
sort
var ones = {}, pairs = 0; for (var i = 0; i < n; i++) { if (ones.hasOwnProperty(ar[i])) { pairs++; delete ones[ar[i]]; } else { ones[ar[i]] = 0; } } return pairs
I have also figured out a reduce way of doing it, but it doesn't reduce the time complexity:
reduce
var ones = {}; return ar.reduce((pairs, i) => { if (ones[i]) { delete ones[i]; return pairs + 1; } else { ones[i] = true; return pairs; } }, 0);
Seems like cookies are disabled on this browser, please enable them to open this website
Sales by Match
You are viewing a single comment's thread. Return to all comments →
Using a JS object provides a faster algorithm because it doesn't require the
sort
operation:I have also figured out a
reduce
way of doing it, but it doesn't reduce the time complexity: