Majority Vote

Published by Helen Yu in

Create a function that returns the majority vote in an array. A majority vote is an element that occurs > N/2 times in an array (where N is the length of the array).

Examples

majority_vote(["A", "A", "B"]) ➞ "A"

majority_vote(["A", "A", "A", "B", "C", "A"]) ➞ "A"

majority_vote(["A", "B", "B", "A", "C", "C"]) ➞ nil

Notes

  • The frequency of the majority element must be strictly greater than 1/2.
  • If there is no majority element, return nil.
  • If the array is empty, return nil.
Watch a quick demo on how Edabit works.