Skip to content

Instantly share code, notes, and snippets.

Say you have a list of a random hashable type (the same type throughout), in an arbitrary order.
How do you find the most common item in the list (along with its count) after the first instance of a given item?
Examples:
[1, 1, 1, 2, 3, 4, 4, 1], 2 -> (4, 2)
[1, 1, 1, 2, 3, 4, 4, 1], 1 -> (1, 3)
["red", "green", "blue", "green", "blue"], "green" -> ("blue", 2)
What we're really looking for here is not just a working solution, but well-crafted, idiomatic Swift.
We want to see an API that fits with Swift APIs, with clear separation of concerns and well-named functions and variables.