-
-
Save renatoalencar/8afc75298423b63594a0a2060104a0ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function findMostFrequent(array, blackListArr) | |
{ | |
if(array.length == 0) | |
return null; | |
var modeMap = {}; | |
var maxEl = array[0], maxCount = 1; | |
// First go through blackList array and set maxEl to the first element that isn't in the blacklist, if every of the numbers in the array is in the blacklist, then just return generateRandom | |
var containsValidNumbers = false; | |
for(var i = 0; i < array.length; i++) { | |
if(!blackListArr.includes(array[i])) { | |
maxEl = array[i]; | |
containsValidNumbers = true; | |
break; | |
} | |
} | |
if(!containsValidNumbers) return await generateRandom(blackListArr); | |
for(var i = 0; i < array.length; i++) | |
{ | |
var el = array[i]; | |
if(blackListArr.includes(el)) continue; | |
if(modeMap[el] == null) | |
modeMap[el] = 1; | |
else | |
modeMap[el]++; | |
if(modeMap[el] > maxCount) | |
{ | |
maxEl = el; | |
maxCount = modeMap[el]; | |
} | |
} | |
await addToBlackList(maxEl); | |
return maxEl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment