← Back to challenges

Find Highest Occurrence

PythonHardarraysnumbersstrings

Instructions

Create a function that takes a list, finds the most repeated element(s) within it and returns it/them in a list. The function should work for both integers and strings mixed together within the input list (e.g. [1, 1, "a"]).

Examples

highest_occurrence([2, 3, 2, 5, 6, 7, 2]) ➞ [2]

highest_occurrence([1, 2, 3, 3, "a", "b", "b", "c"]) ➞ [3, "b"]

highest_occurrence(["it", "keeps", "coding", "on", "or", "it", "gets", "the", "hose"]) ➞ ["it"]

Notes

  • If there is a tie for highest occurrence, return both (see second example above).
  • Don't let integers become strings and/or string become integers in the result.
  • If returning multiple elements, sort result alphabetically with numbers coming before strings.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.