Skip to content

Instantly share code, notes, and snippets.

@dmoreh
Created April 8, 2020 18:53
Show Gist options
  • Save dmoreh/ba524ec60c99920d5c7e6adf9cdf8dee to your computer and use it in GitHub Desktop.
Save dmoreh/ba524ec60c99920d5c7e6adf9cdf8dee to your computer and use it in GitHub Desktop.
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.
@bhatti-saqib
Copy link

I am not been able to understand the Examples. For example, in the 2nd example, 1 appears 4 times in the list but result is shown 1 -> (1, 3). It should be 1 -> (1, 4).
But in the 1st example, the result is correct. 4 appears 2 times so result is 2 -> (4, 2)

@usmanahmed00
Copy link

.

@connor-ricks
Copy link

@bhatti-saqib You might want to re-read the problem again a few times. I think you might have skipped over a crucial part.

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