An array arr consisting of n integers is given. The dominator of arr is the value that occurs in more than half of the elements of arr.
Write a fu...
For further actions, you may consider blocking this person and/or reporting abuse
TypeScript
Swift solution :
And hardcore version :
A solution in JavaScript:
C++ solution
Not tested (written on the phone) but should do the trick
C#
Go Soultion
output:
JavaScript
The edge cases made this one tricky. Here's a working function in Ruby without using Tally
Results
Here's my solution in JavaScript. I'm not sure how to embed Gists, but here is the link to it .... 😥
Doing this taught me about Array.prototype.some. I originally wanted to use .forEach, but needed a way to break out of the loop once we found the dominator -- no need to keep processing once the answer has been found, especially if the scenario was an array of something ridiculous, like 15,000 items.
.tally
is the best!Java: