Tracks
/
Unison
Unison
/
Exercises
/
Majority Element
Majority Element

Majority Element

Medium

Instructions

You've been tasked with identifying if there's a color of candy in a bag that represents the majority share of bag. 🍬 The majority element is one which takes up over half of the space in a collection. Given a list of elements representing the colors, return the color found as the majority, otherwise indicate that no majority has been found.

For a simple list, [Red, Red, Blue, Red, Green], the returned value should be Red because Red occurs 3 times out of a list of 5. But for a list [Red, Red, Blue, Green], there is no majority despite Red being the most common element because 2/4 is not greater than half of the elements.

  • Bonus points: can you generalize your implementation to find the majority element of any type of List?
  • Bonus points: is your implementation performant if there are many different elements to choose from when finding the majority?
  • While you can implement the majorityFinder function in any way you choose, we've provided a few stubs which use the MyStore ability. (The MyStore ability is the same as Store from the standard library, but here we've given it a slightly different name for disambiguation.) You'll need to implement the runWith handler if you choose to use this ability. runWith returns both the result of running the function which uses the MyStore ability and the final state of the MyStore.
  • You can read more about how ability handlers can track state in the ability handler documentation.
Last updated 7 December 2022
Edit via GitHub The link opens in a new window or tab
Unison Exercism

Ready to start Majority Element?

Sign up to Exercism to learn and master Unison with 31 exercises, and real human mentoring, all for free.