Lead Developer: Adobe Experience Manager.
Father of one.
Minnesota.
Occasionally write here: ahmedmusallam.com and there: https://blogs.perficientdigital.com/author/amusallam/
Could not help but rewrite it in javascript to help me understand it better. It's not the syntax, but me trying to wrap my head around it. here is what I have:
functionfindLeader(votes){// after one vote, we have a leaderletleader=votes[0];letcount=1;for(i=1;i<votes.length;i++){letvote=votes[i]if(vote===leader)// the lead may growcount++;else// or shrinkcount--;// and they may be replacedif(count==0){leader=vote;count=1;}}returnleader;}console.log(findLeader(['a','b','a','c','b','c','a','a']));// "a"console.log(findLeader([0,1,1,0,1,0,1,1]));// "1"
Lead Developer: Adobe Experience Manager.
Father of one.
Minnesota.
Occasionally write here: ahmedmusallam.com and there: https://blogs.perficientdigital.com/author/amusallam/
This is awesome!!
Could not help but rewrite it in javascript to help me understand it better. It's not the syntax, but me trying to wrap my head around it. here is what I have:
Looks great! Watch out for
unshift()
which could have an O(n) runtime.ah, good point, updated!