/**
* Return the integer that occurs more than half of the size of an array
*
* @param {number[]} numbers The array of integers.
*
* @throws {Error} If the function gets called with not exactly one argument.
* @throws {TypeError} If the first argument is not an array.
* @throws {TypeError} If the first argument is not an array of integers.
*
* @return {number} -1 if there is no dominator
*
* @example
* console.log(dominator([3,4,3,2,3,1,3,3])); // 3
* console.log(dominator([1,1,1,2,2,2])); // -1
* console.log(dominator([1,1,1,2,2,2,2])); // 2
*/functiondominator(numbers){if(arguments.length!==1){thrownewError("Expected one argument.");}if(!Array.isArray(numbers)){thrownewTypeError("Expected first argument to be an array.");}constoccurrences={};constthreshold=numbers.length/2;for(constnumberofnumbers){if(!Number.isInteger(number)){thrownewTypeError("Expected first argument to be an array of integers.");}if(occurrences[number]){occurrences[number]++;}else{occurrences[number]=1;}if(occurrences[number]>threshold){returnnumber;}}return-1;}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
JavaScript