Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Array Dominator Challenge from Dev.to. Try it yourself, here : https://dev.to/thepracticaldev/daily-challenge-220-what-dominates-your-array-19ne
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