findMaxOccurence

Run Settings
LanguageJavaScript
Language Version
Run Command
function findMaxOccurence(array){ let count=0, candidate=null; for(let x of array){ if(count===0){ candidate=x; count++; } else{ if(candidate===x){ count++; } else{ count-- } } } count=0; for(let x of array){ if(x===candidate){ count++; } } if(count>Math.floor(array.length/2)){ return candidate } else{ return -1 } } console.log(findMaxOccurence([0,0,1,1,0,0,5]));
Editor Settings
Theme
Key bindings
Full width
Lines