Nice post! The great benefit to using a HashMap in this way is that the entire operation takes O(n) time, whereas sorting would usually take something like O(n log n) time.
You could probably also condense it to a one-liner (or maybe two-liner) and keep the O(n) property by doing something like this:
Nice post! The great benefit to using a
HashMap
in this way is that the entire operation takesO(n)
time, whereas sorting would usually take something likeO(n log n)
time.You could probably also condense it to a one-liner (or maybe two-liner) and keep the
O(n)
property by doing something like this:Thanks for sharing!