3
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 ?

2
  • And what is the issue with available code? Commented Oct 9, 2017 at 7:10
  • This doesnt works perfectly ! Commented Oct 10, 2017 at 7:46

5 Answers 5

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;
Sign up to request clarification or add additional context in comments.

5 Comments

nice solution!!
what's the meaning of the 3 dots?
You can also use dataset.filter(x => x === max).length
this solution has O(nlogn) + O(n).
2

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
}

Comments

1

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]));

Comments

1

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);
}

1 Comment

Nice solution for testing.
0
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 compare 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.

1 Comment

So, basically what I did with this solution is use the sort((a, b) => a - b) method to sort the array in descending order which is the most effective incase you're dealing with bigger numbers for example 102, 113, etc where 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 compare 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.