Can someone walk me through this exercise? Write a JavaScript program to find the most frequent item of an array.
I really do not think there is a need for 2 loops in this solution. You can look at this prototyping code which uses a simple data structure called map:
var arr=[3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3];
var map = {};
var mostFrequentElement = arr[0];
function findMostFrequent(){
for(var i = 0; imap[mostFrequentElement]){
mostFrequentElement = arr[i];
}
}
}
alert(mostFrequentElement);
}