DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

2 1

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)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay