Reputation: 77
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 ?
Upvotes: 3
Views: 4914
Reputation: 1
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.
Upvotes: 0
Reputation: 5203
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);
}
Upvotes: 1
Reputation: 48357
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;
Upvotes: 6
Reputation: 608
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
}
Upvotes: 2
Reputation: 386560
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]));
Upvotes: 1