1
// *NOTE: ternary operator is to handle if key is undefined since javascript doesn't have a default object value3
var maxEqualFreq = function (nums) {4
// create cache to count the frequency of each number7
// create cache to count the number of 'number of frequencies'8
// e.g. [1,2,3,2,4,4,3,1,5]9
// { 1: 1 1 number appeared with a frequency of 110
// 2: 3 } 3 numbers appeared with a frequency of 213
// holds the maximum frequency a number has appeared19
// iterate through numbers20
for (let i = 0; i < nums.length; i++) {21
// get current number;24
// subtract one from the current frequency of the current number25
cntFreq[numCnt[num]] = cntFreq[numCnt[num]] ? cntFreq[numCnt[num]] - 1 : -1;27
// increase the count of the current number28
numCnt[num] = numCnt[num] ? numCnt[num] + 1 : 1;30
// add one to the current frequency of the current number31
cntFreq[numCnt[num]] = cntFreq[numCnt[num]] ? cntFreq[numCnt[num]] + 1 : 1;33
// if the current frequency is more than the max frequency update the max frequency34
if (maxFreq < numCnt[num]) maxFreq = numCnt[num];36
// if max frequency is 1 or37
// if the max frequency multiplied by the number of values with that frequency equals the current index or38
// if the result of the 1 less than the max frequency equals to the current index42
maxFreq * cntFreq[maxFreq] === i ||43
(maxFreq - 1) * (cntFreq[maxFreq - 1] + 1) === i