Find elements occurring more than 25% in a given array

1.11K viewsProgrammingalgorithms arrays
0
Answered question
0

Here is a simple JS solution using map and counting the occurrence frequency

/**
 * @param {number[]} arr
 * @return {number}
 */
var findSpecialInteger = function(arr) {
    var map = {};
         for (var i = 0; i < arr.length; i++) {
        if (typeof map[arr[i]] === 'undefined') {
            map[arr[i]] = 0;
        }
        map[arr[i]]++;
    }
         for (var key in map) {
        if (map[key] > arr.length / 4) {
            return key;   
        }
    }
};

Answered question
Write your answer.

Categories