User want's explanation of the code:
Here they select the 1st element of the array and compare it with every element then on.
Then they increment the counter m
every time the same elements occur again, frequency of that element.
Also a variable mf
is kept to keep track of maximum frequency. Compare the elements frequency with maximum frequency and update item
and mf
as per present element's frequency.
var arr1=[3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3]; //array
var mf = 1; //default maximum frequency
var m = 0; //counter
var item; //to store item with maximum frequency
for (var i=0; i<arr1.length; i++) //select element (current element)
{
for (var j=i; j<arr1.length; j++) //loop through next elements in array to compare calculate frequency of current element
{
if (arr1[i] == arr1[j]) //see if element occurs again in the array
m++; //increment counter if it does
if (mf<m) //compare current items frequency with maximum frequency
{
mf=m; //if m>mf store m in mf for upcoming elements
item = arr1[i]; // store the current element.
}
}
m=0; // make counter 0 for next element.
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…