Skip to content

Instantly share code, notes, and snippets.

@Lenewtype
Last active April 7, 2020 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lenewtype/eb705acd9f58d2436053af806ff1472d to your computer and use it in GitHub Desktop.
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
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