Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to find count of the most frequent item of an array. Assume that

ID: 3864438 • Letter: W

Question

Write a program to find count of the most frequent item of an array.

Assume that input is array of integers.

example: input array: [3, -1, -1, -1, 2, 3, -1, 3, -1, 2, 4, 9, 3] ouptut: 5

the program is in Javascript; here's the code I have written so far, it's just double counting some values, and I don't want it to return the duplicates, I want to return the count of the highest number of duplicates. Any help is appreciated, thanks!

functiion mostFrequentItemCount(collection) {

var count = 0;

var duplicates = [];

for(var i = 0; i <collection.length; i++) {

for(var j = i+1; j < collection.length; i++) {

if(collection[i] == collection[j]) {

duplicates.push(collectio[j]);

count++;

}

}

}

return duplicates;

}

Explanation / Answer

Hi

I have fixed the issue and highlighted the code changes below.

function mostFrequentItemCount(collection) {

var count = 1, maxCount = 0;

for(var i = 0; i <collection.length; i++) {
count = 1;
for(var j = i+1; j < collection.length; j++) {
if(collection[i] == collection[j]) {
count++;
}
}
if(maxCount < count){
maxCount = count;
}

}
return maxCount;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote