Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. function electionWinner(votes) {
  2. const vObj = {};
  3. for(let v of votes){
  4. vObj[v] = (vObj[v] || 0) + 1;
  5. }
  6.  
  7. let winners = [];
  8. let maxVotes = 0;
  9.  
  10. for(let name in vObj){
  11. if(vObj[name] > maxVotes){
  12. maxVotes = vObj[name];
  13. winners = [name];
  14. }
  15. else if (vObj[name] === maxVotes){
  16. winners.push(name);
  17. }
  18. }
  19.  
  20. if(winners.length === 1){
  21. return winners[0];
  22. }
  23. winners.sort();
  24. return winners[winners.length - 1];
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement