Last active
November 9, 2022 11:21
-
-
Save sebinsua/522a0725a0c547b9083eebc36662f5b1 to your computer and use it in GitHub Desktop.
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
// 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. |
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
As for grouping:
Create two partitions for odd and even values containing a count of the number of items belonging to these partitions:
Create two partitions for odd and even values containing the sum of the values within these partitions: