admin管理员组

文章数量:1296391

function findMaxOccurence(ar){    
        ar.sort().reverse() // Reverses a sorted array Max to min 
        count = 0;
        for(i=0;i<ar.length;i++){
            ++count
            if(i == ar.length - 1){//break out when last element reached
                break
            }
            if(ar[i+1] != ar[i]){
                break
            }
        }
    return count
}

How to find number of occurrence of highest element in an Javascript Array ?

function findMaxOccurence(ar){    
        ar.sort().reverse() // Reverses a sorted array Max to min 
        count = 0;
        for(i=0;i<ar.length;i++){
            ++count
            if(i == ar.length - 1){//break out when last element reached
                break
            }
            if(ar[i+1] != ar[i]){
                break
            }
        }
    return count
}

How to find number of occurrence of highest element in an Javascript Array ?

Share Improve this question asked Oct 9, 2017 at 6:56 shubham pandeyshubham pandey 772 silver badges6 bronze badges 2
  • And what is the issue with available code? – Rajesh Commented Oct 9, 2017 at 7:10
  • This doesnt works perfectly ! – shubham pandey Commented Oct 10, 2017 at 7:46
Add a ment  | 

5 Answers 5

Reset to default 6

You can use reduce method in order to write a more easy solution.

The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.

let dataset = [2,8,4,8,6,4,7,8];
let max= Math.max(...dataset);
var count = dataset.reduce(function(counter, value) {
    return counter + (value === max);
}, 0);
console.log(count);

Also, you can use filter method by passing a callback function.

let count = dataset.filter(x => x === max).length;

Find the below two methods:

function findMaxOccurence(ar){    
    ar.sort().reverse(); // Reverses a sorted array Max to min
    var count = 1;
    for(var i = 1; i < ar.length; i++){
        if(ar[i] == ar[0])
            count++;
    }
    return count
}

function findMaxOccurence(ar){    
    ar.sort().reverse(); // Reverses a sorted array Max to min
    var count = 1;
    for(var i = 1; i < ar.length; i++){
        if(ar[i] != ar[0])
            break;
        count++;
    }
    return count
}

You could use Array#reduce in a single loop with an object as temporary result set.

function findMaxOccurence(array) {
    return array.reduce(function(r, a) {
        if (!r || a > r.value) {
            return { value: a, count: 1 };
        }
        if (r.value === a) {
            r.count++;
        }
        return r;
    }, undefined).count;
}

console.log(findMaxOccurence([1, 3, 4, 2, 4, 2, 1, 3]));

You can use both these solutions provided below, just remember that the filter solution is a bit faster ^^

//Code

let dataset = [2,8,4,8,6,4,7,8];

let t0 = performance.now();
countWithReduce(dataset);
let t1 = performance.now();
console.log("Call to countWithReduce took " + (t1 - t0) + " milliseconds.")

t0 = performance.now();
countWithFilter(dataset);
t1 = performance.now();
console.log("Call to countWithFilter took " + (t1 - t0) + " milliseconds.")


//Functions

function countWithReduce(arr){
    let max= Math.max(...arr);
    let count = arr.reduce(function(counter, value) {
        return counter + (value === max);
    }, 0);
    console.log(count);
}

function countWithFilter(arr){
    let max= Math.max(...arr);
    let count = arr.filter(x => x === max).length;
    console.log(count);
}

function findMaxOccurence(ar){
ar.sort((a, b) => b - a);
let count = 0;
for(let i = 0; i < ar.length; i++){
if(ar[i] === ar[0]){
count++;
}
}
return count;
}

So, basically what I did with this solution is use the sort((a, b) => a - b) method.

This sorts the array in descending order, which is the most effective in case you're dealing with bigger numbers (for example 102, 113, etc.) for which the reverse method will not be effective.

Then create a count variable to keep track of the number of occurence of the maximum element in the array.

Then run a for loop and pare the elements in the index of ar[i] and add to the count, if that element is equal to ar[0], which will be the maximum element after rearranging the elements in descending order.

本文标签: How to find number of occurrence of highest element in an Javascript ArrayStack Overflow