Level
LeetCode Easy
Recruitment
// sort
var majorityElement = function (nums) {
  return nums.sort((a, b) => a - b)[Math.floor(nums.length / 2)];
};

// hash map
var majorityElement = function (nums) {
  const obj = {};
  const halfSize = nums.length / 2;

  nums.forEach((num) => (obj[num] = ++obj[num] || 1));

  for (let key in obj) {
    if (obj[key] > halfSize) {
      return +key;
    }
  }
};