DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Element Appearing More Than 25% In Sorted Array

Element Appearing More Than 25% In Sorted Array


/**
 * @param {number[]} arr
 * @return {number}
 */
var findSpecialInteger = function (arr) {
  let numMap = new Map();

  for (let i = 0; i < arr.length; i++) {
    if (numMap.get(arr[i])) {
      numMap.set(arr[i], numMap.get(arr[i]) + 1);
    } else {
      numMap.set(arr[i], 1);
    }
  }

  for (let [num, count] of numMap) {
    if (count / arr.length > 0.25) {
      return num;
    }
  }
};

console.log(findSpecialInteger([1, 2, 2, 6, 6, 6, 6, 7, 10]));


Enter fullscreen mode Exit fullscreen mode

Top comments (0)