Last active
April 7, 2020 15:17
-
-
Save Lenewtype/eb705acd9f58d2436053af806ff1472d to your computer and use it in GitHub Desktop.
Array Dominator Challenge from Dev.to. Try it yourself, here : https://dev.to/thepracticaldev/daily-challenge-220-what-dominates-your-array-19ne
This file contains 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
function dominator(arr) { | |
const numCount = {}; | |
let dominator = -1; | |
arr.some(elem => { | |
if(!numCount[elem]) { | |
numCount[elem] = 0; | |
} | |
if(++numCount[elem] > arr.length / 2) { | |
dominator = elem; | |
return true; | |
} | |
}); | |
return dominator; | |
} | |
console.log(dominator([2, 2, 2, 2, 3, 4, 5, 7, 2, 2,3])); | |
let randomArr = Array.from({length: 10}, () => Math.floor(Math.random() * 3)); | |
console.log('Random array: ', randomArr); | |
console.log(dominator(randomArr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment