Skip to content

Instantly share code, notes, and snippets.

@sebinsua
Last active November 9, 2022 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebinsua/522a0725a0c547b9083eebc36662f5b1 to your computer and use it in GitHub Desktop.
Save sebinsua/522a0725a0c547b9083eebc36662f5b1 to your computer and use it in GitHub Desktop.
// Replace items in `as` with a matching `id` with the
// respective item in `bs`, otherwise append new items.
const abs = Object.fromEntries([
...as.map(a => [a.id, a]),
...bs.map(b => [b.id, b])
]);
// The 'trick' is that if you have two entries with the same id
// (in this case, implying that the value was updated within `bs`)
// then with this method the value of the right-most entry will be used.
@sebinsua
Copy link
Author

sebinsua commented Nov 9, 2022

If I get time I will implement an immutable version of Counter.

function total(map) {
  return map.values().reduce((acc, count) => acc + count, 0);
}

function mostCommon(map, n) {
  // ...
  // Not trivial to do well, so I'll come back to this... :)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment